第五篇Android--EditText详解

EditText 字面意思可以编辑的文本。在Android中就是用来接收用户输入的输入框。

1.基本用法

   <EditText
        android:id="@+id/id_phone_edit"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@android:color/transparent"
        android:hint="请输入手机号"
        android:inputType="phone"
        android:lines="1"
        android:maxLength="11"
        android:textColor="#FF151F24"
        android:textColorHint="#FFCECECE"
        android:textCursorDrawable="@drawable/edittext_cursor_bg"
        android:textSize="16sp" />

清除系统默认的EditText背景,添加一个透明背景:

android:background="@android:color/transparent"

未输入时的提示文字和颜色:

android:hint="请输入手机号"

android:textColorHint="#FFCECECE"

输入文字的颜色:

android:textColor="#FF151F24"

修改输入光标的颜色和大小:

android:textCursorDrawable="@drawable/edittext_cursor_bg"

通过drawable目录下定义shape的形式,创建一个drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFAC36"></solid>
    <size
        android:width="1.8dp"
        android:height="17dp" />
    <corners android:radius="0.9dp" />
</shape>

类型:android:inputType="phone"

phone:呼出的软键盘会自动切换成数字键盘,并且限制输入最大长度11个。

textPassword:文本密码,输入的内容会呈现密码输入的形式。

numberPassword:数字密码,只接受数字类型。

2.代码获取输入的文字:

 editText = findViewById(R.id.id_phone_edit);
 String phoneNum = editText.getText().toString().trim();

3.监听EditText文本输入:10086

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after){
                Log.e("nyz","before "+s);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.e("nyz","change "+s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.e("nyz","after "+s);
            }
        });

打印日志:

通过日志可以看出:每一次输入都会调用三个回调函数。

2023-10-12 15:04:24.166 23878-23878/com.example.testview E/nyz: before 
2023-10-12 15:04:24.169 23878-23878/com.example.testview E/nyz: change 1
2023-10-12 15:04:24.169 23878-23878/com.example.testview E/nyz: after 1
2023-10-12 15:04:24.555 23878-23878/com.example.testview E/nyz: before 1
2023-10-12 15:04:24.556 23878-23878/com.example.testview E/nyz: change 10
2023-10-12 15:04:24.556 23878-23878/com.example.testview E/nyz: after 10
2023-10-12 15:04:24.745 23878-23878/com.example.testview E/nyz: before 10
2023-10-12 15:04:24.747 23878-23878/com.example.testview E/nyz: change 100
2023-10-12 15:04:24.748 23878-23878/com.example.testview E/nyz: after 100
2023-10-12 15:04:24.956 23878-23878/com.example.testview E/nyz: before 100
2023-10-12 15:04:24.957 23878-23878/com.example.testview E/nyz: change 1008
2023-10-12 15:04:24.957 23878-23878/com.example.testview E/nyz: after 1008
2023-10-12 15:04:25.374 23878-23878/com.example.testview E/nyz: before 1008
2023-10-12 15:04:25.375 23878-23878/com.example.testview E/nyz: change 10086
2023-10-12 15:04:25.376 23878-23878/com.example.testview E/nyz: after 10086

4.监听软件盘中的回车键:

1)设置回车键的样式 android:imeOptions="",

actionGo:回车键变为"开始"

actionSearch:回车键变为"搜索"

actionDone:默认样式

2)代码中监听回车键:

      InputMethodManager manager = (InputMethodManager)         
          getSystemService(Context.INPUT_METHOD_SERVICE);
    
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                Log.e("nyz", "actionId "+actionId);
                if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
                    //隐藏软键盘
                    if (manager.isActive()) {
                        manager.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    }
                    return true;
                }
                return false;
            }
        });

通过actionId 可以监听按键的类型:

最常用的还是ActionDone

    /**
     * Bits of {@link #IME_MASK_ACTION}: no specific action has been
     * associated with this editor, let the editor come up with its own if
     * it can.
     */
    public static final int IME_ACTION_UNSPECIFIED = 0x00000000;

    /**
     * Bits of {@link #IME_MASK_ACTION}: there is no available action.
     */
    public static final int IME_ACTION_NONE = 0x00000001;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "go"
     * operation to take the user to the target of the text they typed.
     * Typically used, for example, when entering a URL.
     */
    public static final int IME_ACTION_GO = 0x00000002;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "search"
     * operation, taking the user to the results of searching for the text
     * they have typed (in whatever context is appropriate).
     */
    public static final int IME_ACTION_SEARCH = 0x00000003;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "send"
     * operation, delivering the text to its target.  This is typically used
     * when composing a message in IM or SMS where sending is immediate.
     */
    public static final int IME_ACTION_SEND = 0x00000004;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "next"
     * operation, taking the user to the next field that will accept text.
     */
    public static final int IME_ACTION_NEXT = 0x00000005;

    /**
     * Bits of {@link #IME_MASK_ACTION}: the action key performs a "done"
     * operation, typically meaning there is nothing more to input and the
     * IME will be closed.
     */
    public static final int IME_ACTION_DONE = 0x00000006;

5.EditText 设置其他类型的事件监听:

       editText.setOnTouchListener((v, event) -> {
            //todo
            return false;
        });
        editText.setOnLongClickListener(v -> {
            //todo
            return false;
        });
        editText.setOnClickListener(v -> {
            
        });
相关推荐
666xiaoniuzi4 小时前
深入理解 C 语言中的内存操作函数:memcpy、memmove、memset 和 memcmp
android·c语言·数据库
沐言人生8 小时前
Android10 Framework—Init进程-8.服务端属性文件创建和mmap映射
android
沐言人生8 小时前
Android10 Framework—Init进程-9.服务端属性值初始化
android·android studio·android jetpack
沐言人生9 小时前
Android10 Framework—Init进程-7.服务端属性安全上下文序列化
android·android studio·android jetpack
追光天使9 小时前
【Mac】和【安卓手机】 通过有线方式实现投屏
android·macos·智能手机·投屏·有线
小雨cc5566ru9 小时前
uniapp+Android智慧居家养老服务平台 0fjae微信小程序
android·微信小程序·uni-app
一切皆是定数10 小时前
Android车载——VehicleHal初始化(Android 11)
android·gitee
一切皆是定数10 小时前
Android车载——VehicleHal运行流程(Android 11)
android
problc10 小时前
Android 组件化利器:WMRouter 与 DRouter 的选择与实践
android·java
图王大胜11 小时前
Android SystemUI组件(11)SystemUIVisibility解读
android·framework·systemui·visibility