Project 內容
Login.axml
<?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<?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.csnamespace 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.csnamespace 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 傳遞參數-3](https://c6.staticflickr.com/8/7376/27741453301_4c66678468.jpg)
![[X.Andriod] Activity 傳遞參數-4](https://c7.staticflickr.com/8/7350/27214628014_a5bb1ce103.jpg)
![[X.Andriod] Activity 傳遞參數-5](https://c5.staticflickr.com/8/7130/27214628124_f60a194e80.jpg)
![[X.Andriod] Activity 傳遞參數-1](https://c6.staticflickr.com/8/7321/27741238541_2735a04299_z.jpg)
![[X.Andriod] Activity 傳遞參數-2](https://c4.staticflickr.com/8/7064/27741238651_07f9946db7_z.jpg)
2 則留言:
看來可以考慮系統架構了,登入都完成了,第一版 APP 看來就快釋出囉
沒這麼快啦,單純練習 Activity 之間傳值而已啦,^^
張貼留言