同一个Activity先后加载2个Layout,从layout1取值传入layout2
同一个Activity先后加载2个Layout,从layout1取值传入layout2
没啥技术含量,就权当丰富下mono for android的小代码.
Main.xaml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/txtuser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号:" /> <EditText android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/txtuser" /> <TextView android:id="@+id/txtpass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/username" android:text="密码:" /> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/txtpass" android:password="true" /> <Button android:id="@+id/ok" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="OK,Now Next" /> <Button android:id="@+id/cancel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Cancel" /> </LinearLayout>
Info.xaml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="用户名:" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/username2" /> <TextView android:text="密码:" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/password2" /> </LinearLayout>
Activity1.cs
using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; namespace layoutAvaluetolayoutB { [Activity(Label = "layoutAvaluetolayoutB", MainLauncher = true, Icon = "@drawable/icon")] public class Activity1 : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); EditText username = FindViewById<EditText>(Resource.Id.username); EditText password = FindViewById<EditText>(Resource.Id.password); Button btnOK = FindViewById<Button>(Resource.Id.ok); //btnOK.Click += delegate { showinfo(this,username.Text, password.Text); };//版本1 btnOK.Click += delegate { showinfo(username, password); };//版本2 //btnOK.Click += (sender, e) => //{ // EditText username = FindViewById<EditText>(Resource.Id.username); // EditText password = FindViewById<EditText>(Resource.Id.password); // AlertDialog.Builder dlg = new AlertDialog.Builder(this); // dlg.SetTitle("提示"); // dlg.SetMessage(string.Format("你输入的账号是:{0},密码是:{1}", username.Text, password.Text)); // dlg.SetPositiveButton("确定", delegate { }); // dlg.Show(); //}; } #region //版本1 //private Activity context = null; //public void showinfo(Activity ac, string username, string password) //{ // this.context = ac; // context.SetContentView(Resource.Layout.Info); // TextView username2 = FindViewById<TextView>(Resource.Id.username2); // TextView password2 = context.FindViewById<TextView>(Resource.Id.password2); // username2.SetText(username, TextView.BufferType.Normal); // password2.SetText(password, TextView.BufferType.Normal); // //context.SetContentView(Resource.Layout.Info); //} #endregion //版本2 public void showinfo(EditText username, EditText password) { SetContentView(Resource.Layout.Info); FindViewById<TextView>(Resource.Id.username2).SetText("用户名:" + username.Text, TextView.BufferType.Normal); FindViewById<TextView>(Resource.Id.password2).Text += password.Text; } } }