星期二, 6月 21, 2016

[X.Andriod] EditText

看 Xamarin.Andriod 範例時,在 XML 內都有看見 EditText 控制項,可是在工具箱內就是找不到

[X.Andriod] EditText-1

後來才知道原來是 Text Fields 分類內的 Plain Text 控件

官網上看見兩個範例,整合在一起練習
[X.Andriod] EditText-3
<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtInput"/>
    <TextView
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="47.5dp"
        android:id="@+id/txtResult" />
</LinearLayout>
namespace EditTextPractice
{
    [Activity(Label = "EditTextPractice", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            EditText txtInput = FindViewById<EditText>(Resource.Id.txtInput);
            TextView txtResult = FindViewById<TextView>(Resource.Id.txtResult);

            txtInput.TextChanged += (sender, e) => {
                txtResult.Text = e.Text.ToString();};

            txtInput.KeyPress += (sender, e) =>
            {
                e.Handled = false;
                if (e.KeyCode == Keycode.Enter)
                    Toast.MakeText(this, txtInput.Text, ToastLength.Short).Show();    
                e.Handled = true;
            };
        }
    }
}
[X.Andriod] EditText-2

沒有留言:

張貼留言