Android(十一):EditText 输入框

基本属性

属性 描述
hint 默认提示文本
textColorHint 默认提示文本颜色
selectAllOnFocus 获得焦点后全选组件内所有文本内容
minLines 最小行数
maxLines 最大行数
inputType 输入类型

inputType 可选参数

  • 文本类型,多为大写、小写和数字符号
    android:inputType="none"  
    android:inputType="text"  
    android:inputType="textCapCharacters"  
    android:inputType="textCapWords"  
    android:inputType="textCapSentences"  
    android:inputType="textAutoCorrect"  
    android:inputType="textAutoComplete"  
    android:inputType="textMultiLine"  
    android:inputType="textImeMultiLine"  
    android:inputType="textNoSuggestions"  
    android:inputType="textUri"  
    android:inputType="textEmailAddress"  
    android:inputType="textEmailSubject"  
    android:inputType="textShortMessage"  
    android:inputType="textLongMessage"  
    android:inputType="textPersonName"  
    android:inputType="textPostalAddress"  
    android:inputType="textPassword"  
    android:inputType="textVisiblePassword"  
    android:inputType="textWebEditText"  
    android:inputType="textFilter"  
    android:inputType="textPhonetic" 
    
  • 数值类型
    android:inputType="number"  
    android:inputType="numberSigned"  
    android:inputType="numberDecimal"  
    android:inputType="phone"//拨号键盘  
    android:inputType="datetime"  
    android:inputType="date"//日期键盘  
    android:inputType="time"//时间键盘
    

获取键盘

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">
    
    <EditText
        android:id="@+id/edit01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入" />
    
    <EditText
        android:id="@+id/edit02"
        android:layout_below="@id/edit01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入" />
    
    <Button
        android:id="@+id/btn01"
        android:layout_below="@id/edit02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取焦点" />
    
    <Button
        android:id="@+id/btn02"
        android:layout_below="@id/btn01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清除焦点" />
    
    <Button
        android:id="@+id/btn03"
        android:layout_below="@id/btn02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="无需指定输入框,显示/隐藏键盘" />
    
    <Button
        android:id="@+id/btn04"
        android:layout_below="@id/btn03"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="最近一次获取的焦点在指定的标签上才能显示该标签的键盘" />
    
    <Button
        android:id="@+id/btn05"
        android:layout_below="@id/btn04"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="隐藏键盘" />
    
</RelativeLayout>
var imm = (InputMethodManager)GetSystemService(InputMethodService);

var edit01 = (EditText)FindViewById(Resource.Id.edit01);
var btn01 = (Button)FindViewById(Resource.Id.btn01);
var btn02 = (Button)FindViewById(Resource.Id.btn02);
var btn03 = (Button)FindViewById(Resource.Id.btn03);
var btn04 = (Button)FindViewById(Resource.Id.btn04);
var btn05 = (Button)FindViewById(Resource.Id.btn05);

// 获取焦点
if (btn01 == null) return;
btn01.Click += (s, e) => { edit01?.RequestFocus(); };
// 清除焦点
if (btn02 == null) return;
btn02.Click += (s, e) => { edit01?.ClearFocus(); };
// 无需指定输入框,显示/隐藏键盘
if (btn03 == null) return;
btn03.Click += (s, e) => { imm?.ToggleSoftInput(0, 0); };
// 最近一次获取的焦点在指定的标签上才能显示该标签的键盘
if (btn04 == null) return;
btn04.Click += (s, e) => { imm?.ShowSoftInput(edit01, 0); };
// 隐藏键盘
if (btn05 == null) return;
btn05.Click += (s, e) => { imm?.HideSoftInputFromWindow(edit01?.WindowToken, 0); };

插入图片

var btn01 = (Button)FindViewById(Resource.Id.btn01);
var edit01 = (EditText)FindViewById(Resource.Id.edit01);
if (btn01 == null) return;
btn01.Click += (s, e) =>
{
    // 先把资源转换成位图
    var bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.mini_logo);
    // 定义一个可输入EditText的字符串对象
    var spannableString = new SpannableString("logo");
    // 将可输入的EditText框替换成位图对象
    var span = new ImageSpan(this, bitmap);
    spannableString.SetSpan(span, 0, 4, SpanTypes.ExclusiveExclusive);
    edit01?.Append(spannableString);
};

带清空功能的输入框(2种)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <!--第一种-->
    <EditText
        android:id="@+id/edit01"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/input"
        android:singleLine="true"
        android:hint="请输入"
        android:textIsSelectable="true" />

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/edit01"
        android:layout_marginTop="10dp">
    
        <View
            android:id="@+id/view00"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#ff0000" />
    
        <View
            android:id="@+id/view01"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#00ff00" />
    
        <View
            android:id="@+id/view02"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#0000ff" />
    
        <View
            android:id="@+id/view03"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#ff00ff" />
    
    </LinearLayout>
    
    <!--第二种-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/LinearLayout01"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:background="@drawable/input">
        <EditText
            android:id="@+id/edit02"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:singleLine="true"
            android:hint="请输入"
            android:textIsSelectable="true"
            android:layout_weight="1"/>
        <ImageButton
            android:id="@+id/close"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="15dp"
            android:background="@drawable/close"
            android:visibility="invisible"/>
    </LinearLayout>
</RelativeLayout>
  • 第一种
    // MainActivity.cs
    var view00 = FindViewById(Resource.Id.view00);
    var view01 = FindViewById(Resource.Id.view01);
    var view02 = FindViewById(Resource.Id.view02);
    var view03 = FindViewById(Resource.Id.view03);
    
    var edit01 = (EditText)FindViewById(Resource.Id.edit01);
    if (edit01 == null) return;
    var unused = new EditTextWithDel(this, edit01, view00, view01, view02, view03);
    
    // EditTextWithDel.cs
    using System.Linq;
    using Android.App;
    using Android.Content;
    using Android.Provider;
    using Android.Views;
    using Android.Views.InputMethods;
    using Android.Widget;
    
    namespace android_by_csharp
    {
        public class EditTextWithDel : EditText
        {
            private static View _view00;
            private static View _view01;
            private static View _view02;
            private static View _view03;
    
            private static Context _context;
            private const int CloseW = 60;
            private const int CloseH = 60;
    
            public EditTextWithDel(Context context, EditText editText, View view00 = null, View view01 = null,
                View view02 = null, View view03 = null) : base(context)
            {
                // 区域
                _view00 = view00;
                _view01 = view01;
                _view02 = view02;
                _view03 = view03;
    
                _context = context;
                Init(editText);
            }
    
            private static void Init(EditText editText)
            {
                // 清空图标
                var closeIcon = _context.Resources?.GetDrawable(Resource.Drawable.close);
                closeIcon?.SetBounds(0, 0, CloseW, CloseH);
    
                // 键盘
                var imm = (InputMethodManager)Application.Context.GetSystemService(Context.InputMethodService);
    
                editText.Click += (s, e) =>
                {
                    
                };
                
                // 点击事件
                editText.Touch += (s, e) =>
                {
                    if (_view00.LayoutParameters == null) return;
                    _view00.LayoutParameters.Width = editText.PaddingLeft;
                    _view00.LayoutParameters = _view00.LayoutParameters;
                
                    if (_view01.LayoutParameters == null) return;
                    _view01.LayoutParameters.Width = editText.Width - editText.PaddingLeft - editText.PaddingRight - CloseW;
                    _view01.LayoutParameters = _view01.LayoutParameters;
                
                    if (_view02.LayoutParameters == null) return;
                    _view02.LayoutParameters.Width = CloseW;
                    _view02.LayoutParameters = _view02.LayoutParameters;
                
                    if (_view03.LayoutParameters == null) return;
                    _view03.LayoutParameters.Width = editText.PaddingRight;
                    _view03.LayoutParameters = _view03.LayoutParameters;
                
                    if (e.Event == null) return;
                    
                    // 点击清空图标所处位置
                    if (e.Event.GetX() > editText.Width - editText.PaddingRight - CloseW &&
                        e.Event.GetX() < editText.Width - editText.PaddingRight)
                    {
                        editText.Text = "";
                    }
                    // 其他位置(获取焦点和弹出软键盘)
                    else
                    {
                        editText.RequestFocus();
                        imm?.ShowSoftInput(editText, 0);
                    }
                };
    
                // 监听输入(显隐清空图标)
                editText.TextChanged += (s, e) =>
                {
                    // Console.WriteLine($"{s}, {e}, {e.Text}, {e.Start}, {e.BeforeCount}, {e.AfterCount}");
                    editText.SetCompoundDrawables(null, null, e.Text != null && !e.Text.Any() ? null : closeIcon, null);
                };
            }
        }
    }
    
  • 第二种
    // MainActivity.cs
    var edit02 = (EditText)FindViewById(Resource.Id.edit02);
    var close = (ImageView)FindViewById(Resource.Id.close);
    if (edit02 == null || close == null) return;
    var unused1 = new ClearText(this, edit02, close);
    
    // ClearText.cs
    using System;
    using System.Linq;
    using Android.Content;
    using Android.Views;
    using Android.Widget;
    
    namespace android_by_csharp
    {
        public class ClearText : View
        {
            public ClearText(Context context, EditText editText, ImageView close) : base(context)
            {
                Init(editText, close);
            }
    
            private static void Init(EditText editText, ImageView close)
            {
                close.Click += (s, e) =>
                {
                    editText.Text = "";
                };
                // 监听输入(显隐清空图标)
                editText.TextChanged += (s, e) =>
                {
                    close.Visibility = e.Text != null && !e.Text.Any() ? ViewStates.Invisible : ViewStates.Visible;
                    // Console.WriteLine($"{s}, {e}, {e.Text}, {e.Start}, {e.BeforeCount}, {e.AfterCount}");
                };
            }
        }
    }
    
  • 效果
    Android(十一):EditText 输入框
上一篇:MetadataReader


下一篇:yii2 pjax使用