星期三, 6月 22, 2016

[X.Andriod] Activity 傳遞參數

簡易登錄介面,練習從 Activity 傳遞參數至一個 Activity,並把參數內容顯示出來

Project 內容
[X.Andriod] Activity 傳遞參數-3


Login.axml

[X.Andriod] Activity 傳遞參數-4

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1">
        <TextView
            android:text="使用者名稱:"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/textView1"
            android:textSize="20dp"
            android:gravity="center" />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/txtUserName"
            android:hint="請輸入使用者名稱"
            android:inputType="text" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2">
        <TextView
            android:text="使用者密碼:"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/textView1"
            android:textSize="20dp" />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/txtPWD"
            android:hint="請輸入使用者密碼"
            android:inputType="numberPassword" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout3">
        <Button
            android:text="登錄"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/btnLogin" />
    </LinearLayout>
</LinearLayout>
LoginInfo.axml

[X.Andriod] Activity 傳遞參數-5

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <TextView
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtInfo"
        android:textSize="40dp" />
</LinearLayout>
MainActivity.cs
namespace LoginPractice
{
    [Activity(Label = "登錄小範例", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Login);

            EditText txtUserName = FindViewById<edittext>(Resource.Id.txtUserName);
            EditText txtPWD = FindViewById<edittext>(Resource.Id.txtPWD);

            Button btn = FindViewById<button>(Resource.Id.btnLogin);
            btn.Click += (sender, e) => {

            string PWD = txtPWD.Text.Trim();
            if (PWD != "123456")
            {
                txtPWD.Text = string.Empty;
                return;
            }

            Bundle b = new Bundle();
            b.PutString("UserName", txtUserName.Text.Trim());

            Intent i = new Intent(this , typeof(InfoActivity));
            i.PutExtras(b);

            this.StartActivity(i);
            // 關閉該 Activity,避免登錄成功後,還可以回到該 Activity
            this.Finish();};
        }
    }
}
InfoActivity.cs
namespace LoginPractice
{
    [Activity(Label = "登錄成功資訊")]
    public class InfoActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.LoginInfo);
   
            // 方法一:利用 Bundle 來取參數
            //string UserName = this.Intent.Extras.GetString("UserName") ?? "參數無資料";
            // 方法二:利用 Intent 來取參數
            string UserName = this.Intent.GetStringExtra("UserName") ?? "參數無資料";

            TextView txt = FindViewById<TextView>(Resource.Id.txtInfo);
            txt.Text = $"使用者:{UserName},登錄成功";
        }
    }
}
程式執行畫面
[X.Andriod] Activity 傳遞參數-1

登錄成功
[X.Andriod] Activity 傳遞參數-2

2 則留言:

  1. 看來可以考慮系統架構了,登入都完成了,第一版 APP 看來就快釋出囉

    回覆刪除
  2. 沒這麼快啦,單純練習 Activity 之間傳值而已啦,^^

    回覆刪除