This recipe shows how to use EditText control to capture text entered by a user.
Recipe
- Create a new Xamarin.Android application named EditTextDemo.
- In Main.xml to include an EditText and a TextView.
<?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"> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
- In MainActivity.cs, get references to the EditText and the TextView.
var editText = FindViewById<EditText> (Resource.Id.editText); var textView = FindViewById<TextView> (Resource.Id.textView);
- Implement the TextChanged event to capture the text from the EditText and write it to the TextView.
editText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => { textView.Text = e.Text.ToString (); };
Additional Information
The EditText control also has events for handling other user interactions more such as when a key is pressed and before and after text is changed.
