Hello. Xamarin.Android . The user presses the button. A dialog box appears. Enters the text. After clicking on the OK button in the dialog box, the text field in the main window should be populated with the entered text. An error occurs. At runtime, after entering text, the editText1 variable is empty, its value is null . This can be seen in the screenshot. Maybe someone faced this problem, tell me. Thank.
Here is the code.
Dialog.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"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText1" /> </LinearLayout> Main.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:id="@+id/root" android:minWidth="25px" android:minHeight="25px"> <LinearLayout android:orientation="vertical" android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/linearLayoutMain"> <LinearLayout android:orientation="horizontal" android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="2dp" android:background="#F5F5F5" android:weightSum="100"> <Button android:text="ok" android:layout_width="wrap_content" android:layout_height="50dp" android:id="@+id/button1" android:layout_weight="100" android:layout_margin="2dp" android:background="#778899" android:textColor="#FFFFFF" android:textSize="18sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#F5F5F5"> <LinearLayout android:orientation="horizontal" android:minWidth="25px" android:minHeight="25px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="2dp" android:background="#667788"> <TextView android:id="@+id/textView1" android:layout_width="200dp" android:layout_height="35dp" android:layout_margin="2dp" android:gravity="center" android:background="#FFFFFF" android:textColor="#FF0000" android:textSize="16sp" android:textStyle="bold" /> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> MainActivity.cs
using Android.App; using Android.Widget; using Android.OS; namespace App1 { [Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { Button button1 = null; TextView textView1 = null; private void funAlert(TextView arg) { AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.Create(); alert.SetTitle("ENTER TEXT"); alert.SetView(Resource.Layout.Dialog); alert.SetNeutralButton("Cancel", (sender, e) => { alert.Dispose(); }); alert.SetPositiveButton("Ok", (sender, e) => { EditText editText1 = FindViewById<EditText>(Resource.Id.editText1); arg.Text = editText1.Text; }); alert.Show(); } protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); button1 = FindViewById<Button>(Resource.Id.button1); textView1 = FindViewById<TextView>(Resource.Id.textView1); button1.Click += (sender, e) => { funAlert(textView1); }; } } } 
FindViewByIdis not called by the dialog, but by theActivity— and it does not haveId.editText1. Take a closer look at the dock for the function - woesss