layout1.axml:兩個 Button,都是呼叫 Activity2
<?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"> <Button android:text="MS 產品" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnMS" /> <Button android:text="非 MS 產品" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnNonMS" /> </LinearLayout>layout2.axml:CheckBox 讓使用者勾選並確認回傳
<?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:id="@+id/cbLayout"> <CheckBox android:text="SQL Server 2016" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/checkBox1" android:textSize="20dp" /> <CheckBox android:text="Windows Server 2016" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/checkBox2" android:textSize="20dp" /> <CheckBox android:text="ASP.NET Core" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/checkBox3" android:textSize="20dp" /> <CheckBox android:text="Xamarin" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/checkBox4" android:textSize="20dp" /> <CheckBox android:text="Hyper-V" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/checkBox5" android:textSize="20dp" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="39.0dp" android:id="@+id/linearLayout1"> <Button android:text="確定" android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/btnOK" /> <Button android:text="取消" android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/btnCancel" /> </LinearLayout> </LinearLayout>
Activity1.cs
namespace ActivityBack { [Activity(Label = "Activity1", MainLauncher = true)] public class Activity1 : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.layout1); FindViewById<Button>(Resource.Id.btnMS).Click += (sender, e) => { // StartActivityForResult 第二參數是故意塞 btnMS id 進去 // 只要是大於零的整數都是 OK 的 this.StartActivityForResult( typeof(Activity2), Resource.Id.btnMS); }; FindViewById<Button>(Resource.Id.btnNonMS).Click += (sender, e) => { // 故意也呼叫 Activity2,要在 OnActivityResult 內判斷是由哪個 Button 觸發的 this.StartActivityForResult( typeof(Activity2), Resource.Id.btnNonMS); }; } protected override void OnActivityResult( int requestCode, [GeneratedEnum] Result resultCode, Intent data) { if (resultCode != Result.Ok) return; // 不是由 btnMS 進入 Activity 並回傳點選結果,就要擋下來 if (requestCode != Resource.Id.btnMS) { string message = $"邏輯異常喔,requestCode 為 { Resource.Id.btnNonMS}"; Toast.MakeText(this, message, ToastLength.Short).Show(); return; } StringBuilder sb = new StringBuilder(); // 第一參數:requestCode 即為 StartActivityForResult 第二參數,可以透過該參數來判斷當初的來源是誰 sb.AppendLine($"requestCode:{requestCode}"); // 第三參數 data:從 Activity2 回傳的 Intent sb.AppendLine(data.GetStringExtra("RetValue") ?? "沒有相關資料"); Toast.MakeText(this, sb.ToString(), ToastLength.Long).Show(); } } }Activity2.cs
namespace ActivityBack { [Activity(Label = "Activity2")] public class Activity2 : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.layout2); FindViewById<Button>(Resource.Id.btnOK).Click += (sender, e) => { Intent i = new Intent(); i.PutExtra("RetValue", CheckBoxState()); SetResult(Result.Ok, i); this.Finish(); }; FindViewById<Button>(Resource.Id.btnCancel).Click += (sender, e) => { SetResult(Result.Canceled); this.Finish(); }; } private string CheckBoxState() { LinearLayout layout = FindViewById<LinearLayout>(Resource.Id.cbLayout); int ctls = layout.ChildCount; StringBuilder sb = new StringBuilder(); for (int i = 0; i < ctls; i++) { View v = layout.GetChildAt(i); if ((v is CheckBox) == false) continue; CheckBox cb = v as CheckBox; sb.AppendLine((cb.Checked == true ? "已勾選" : "未勾選") + ":" + cb.Text); } return sb.ToString(); } } }Resource.Designer.cs 內的 btnMS ID 和 btnNonMS ID,ID 資料型別為 int 喔
在 Activity2 中進行勾選
從 btnNonMS 進入 Activity2 的結果
從 btnMS 進入 Activity2 後,使用者勾選的結果
沒有留言:
張貼留言