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.

enter image description here

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); }; } } } 
  • I do not know sharps, but I can assume that FindViewById is not called by the dialog, but by the Activity — and it does not have Id.editText1 . Take a closer look at the dock for the function - woesss
  • Probably it would be more correct to transfer text input from a dialogue to an activity either through an intent or through callback. - plesser

1 answer 1

Find the FindViewById<EditText>(Resource.Id.editText1); element FindViewById<EditText>(Resource.Id.editText1); as it is written now, it searches for this editText1 in the interface of the main activation, and not the dialogue.

Method alert.Show(); returns a dialog object built by this builder. Save it.

Then you need to call GetWindow() for the saved dialog, and call this object FindViewById() . I suppose it will look something like this (do not kick, I see C # and Xamarin for the first time):

 // где-то в начале AlertDialog dialog; EditText editText1; // при создании диалога alert.SetPositiveButton("Ok", (sender, e) => { // editText1 уже найдена, см. ниже arg.Text = editText1.Text; }); // при открытии диалога dialog = alert.Show(); editText1 = dialog.GetWindow().FindViewById<EditText>(Resource.Id.editText1);