id | title | brief | article | sdk | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
EC009100-EAE0-0BDA-3AC9-60BBF78854EB |
Capture User Input Text |
This recipe shows how to use EditText control to capture text entered by a user. |
|
|
- Create a new Xamarin.Android application named EditTextDemo.
- In Main.xml to include an
EditText
and aTextView
.
<?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 theTextView
.
var editText = FindViewById<EditText> (Resource.Id.editText);
var textView = FindViewById<TextView> (Resource.Id.textView);
- Implement the
TextChanged
event to capture the text from theEditText
and write it to theTextView
.
editText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => {
textView.Text = e.Text.ToString ();
};
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.