一、编辑框EditText

inputType: 指定输入的文本类型。可使用“|” 把多种文本类型拼接起来。
maxLength: 指定文本允许输入的最大长度。
hint: 指定提示文本的内容
textColorHint: 指定提示文本的颜色。
输入类型 说明
text 文本密码。显示时用圆点".“代替
number 整型数
numberSigned 带符号的数字。允许在开头带符号”-"
numberDecimal 带小数的数字
numberPassword 数字密码。显示时用圆点"."代替
datetime 时间日期格式。除了数字外还允许输入横线、斜杠、空格、冒号
date 日期格式。输了数字外,还允许输入横线、斜杠、空格、冒号

time 时间格式。除了数字外,还允许输入冒号
需求: 修改原生编辑框的样式
布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    // 原生
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="默认边框"
        android:inputType="text"
        />
        // 去掉原生边框
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="我的边框不见了"
        android:layout_marginTop="5dp"
        android:background="@null"
        android:inputType="textPassword"
        />
        // 自定义边框
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:hint="我的边框是圆角"
        android:background="@drawable/editext_selector"
        android:inputType="textPassword"
        />
</LinearLayout>

drawable/shape_edit_focus.xml : 编辑框有焦点的时候显示的样式

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<!--    指定形状内部的填充颜色-->
    <solid android:color="#ffffff" />
<!--    指定形状轮廓的粗细与颜色-->
    <stroke android:width="1dp"
        android:color="#0000ff" />
<!--    指定了形状四个圆角的半径-->
    <corners android:radius="5dp" />
<!--    指定形状四个方向的间距-->
    <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp"/>

</shape>

drawable/shape_edit_normal.xml : 编辑框无焦点的时候显示的样式

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<!--    指定形状内部的填充颜色-->
    <solid android:color="#ffffff" />
<!--    指定形状轮廓的粗细与颜色-->
    <stroke android:width="1dp"
        android:color="#aaaaaa" />
<!--    指定了形状四个圆角的半径-->
    <corners android:radius="5dp" />
<!--    指定形状四个方向的间距-->
    <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp"/>

</shape>

drawable/editext_selector.xml: 样式选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/shape_edit_focus"/>
    <item android:drawable="@drawable/shape_edit_normal"/>
</selector>

效果展示:
在这里插入图片描述

二、焦点变更监听器

  • 编辑框点击两次后才会出触发点击事件,因为第一次 点击只触发焦点变更事件,第二次点击才触发点击事件。
    若要判断是否切换编辑框输入,应当监听焦点变更事件,而非监听点击事件。
    调用编辑框对象的setOnFocusChangeListener方法,即可在光标切换之时(获得光标和失去光标)触发焦点变更事件。
    需求:手机号未输满11位,就点击密码框,次时校验不通过,一般弹出提示文字,一边把焦点拉回手机框。
    在这里插入图片描述
    布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入11位手机号码"
        android:inputType="number"
        android:maxLength="11"
        android:background="@drawable/editext_selector"
        />
    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/editext_selector"
        android:hint="请输入6位密码"
        android:inputType="numberPassword"
        android:maxLength="6"
        />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        />
</LinearLayout>

选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/shape_edit_focus"/>
    <item android:drawable="@drawable/shape_edit_normal"/>
</selector>

业务代码实现:

public class EditFocusActivity extends AppCompatActivity implements View.OnFocusChangeListener{

    private EditText et_phone;
    private EditText et_password;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_focus);

        et_phone = findViewById(R.id.et_phone);
        et_password = findViewById(R.id.et_password);

        et_password.setOnFocusChangeListener(this);
    }

    //焦点变更监听器
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            String phone = et_phone.getText().toString();
            // 手机号不足11位
            if (TextUtils.isEmpty(phone) || phone.length() < 11) {
                // 手机号码编辑框请求焦点,也就是把光标移回手机号码编辑框
                et_phone.requestFocus();
                Toast.makeText(this, "请输入11位手机号码", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

效果展示:
在这里插入图片描述

三、 文本变化监听器

  • 判断手机号输入满11位后自动关闭软键盘,或者密码输入满6位后自动关闭软键盘,此时要注册文本变化监听器
    达到指定位数便自动关闭键盘的功能,可以再分解为两个独立的功能点。

如何关闭软键盘
如何判断已输入的文字达到指定位数
在这里插入图片描述
步骤:
调用编辑框对象的addTextChangedListener方法即可注册文本监听器。
文本监听器的接口名称为TextWatcher,该接口提供了三个监控方法:
beforeTextChanged: 在文本改变之前触发
onTextChanged: 在文本改变过程中触发
afterTextChanged:在文本改变知乎触发

实现:
布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入11位时自动隐藏输入法"
        android:inputType="number"
        android:maxLength="11"
        android:background="@drawable/editext_selector"
        />
    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/editext_selector"
        android:hint="请输入6位时自动隐藏输入法"
        android:inputType="numberPassword"
        android:maxLength="6"
        />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        />
</LinearLayout>

工具类:

/**
 * @author liuzhihao
 * @create 2023-02-26 15:31
 */
public class ViewUtil {

    public static void hideOneInputMethod(Activity act, View v) {
        // 从系统服务中获取输入法管理器
        InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
        // 关闭屏幕上的输入法软键盘
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}

业务代码:

public class EditHideActivity extends AppCompatActivity {

    private static final String TAG = "liuzhihao";
    
    private EditText et_phone;
    private EditText et_password;
    private Button btn_login;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_hide);

        et_phone = findViewById(R.id.et_phone);
        et_password = findViewById(R.id.et_password);

        et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11));
        et_password.addTextChangedListener(new HideTextWatcher(et_password, 6));
    }

    // 定义一个编辑框监听器,在输入文本达到指定长度时自动隐藏输入法
    private class HideTextWatcher implements TextWatcher {

        private EditText mView;// 声明一个编辑框对象
        private int mMaxLength; // 声明一个最大长度变量

        public HideTextWatcher(EditText editText, int maxLength) {
            this.mView = editText;
            this.mMaxLength = maxLength;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            Log.d(TAG, "beforeTextChanged: ");
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.d(TAG, "onTextChanged: ");
        }

        // 在编辑框的输入文本变化后触发
        @Override
        public void afterTextChanged(Editable s) {
            Log.d(TAG, "afterTextChanged: ");
            // 获得已输入的文本字符串
            String str = s.toString();
            // 输入文本达到11位(手机号码),或者达到6位(密码)时关闭输入法
            if (str.length() ==  mMaxLength ) {
                // 隐藏输入法软键盘
                ViewUtil.hideOneInputMethod(EditHideActivity.this, mView);
            }
        }
    }

}

效果实现:当第一个输入框输满11位时软键盘自动收回
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐