Android开发中限制EditText输入字符的几种实用方法

TextWatcher 可以用于监听文本变化,并在输入时实施限制。例如检查输入是否包含某些特定字符,并删掉不是字母或数字的字符。

TextWatcher 可以用于监听文本变化,并在输入时实施限制。例如检查输入是否包含某些特定字符,并删掉不是字母或数字的字符。

Android 开发中,经常需要限制EditText的输入字符,以下是几种不同的实现方法。

使用InputFilter

通过实现InputFilter接口来限制输入。例如限制输入长度为 10。

InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(10);
editText.setFilters(filters);

正则判断是否输入的是中文:

editText.setFilters(new InputFilter[]{
    new InputFilter() {
        @Override
        public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
            String regex = "^[\u4E00-\u9FA5]+$";
            boolean isChinese = Pattern.matches(regex, charSequence.toString());
            if (!Character.isLetterOrDigit(charSequence.charAt(i)) || isChinese) {
                return "";
            }
            return null;
        }
    }
});

使用TextWatcher

TextWatcher可以用于监听文本变化,并在输入时实施限制。例如检查输入是否包含某些特定字符,并删掉不是字母或数字的字符。

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String editable = evPwd.getText().toString();
        String regEx = "[^a-zA-Z0-9]";  //只能输入字母或数字
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(editable);
        String str = m.replaceAll("").trim();    //删掉不是字母或数字的字符
        if(!editable.equals(str)){
            evPwd.setText(str);  //设置EditText的字符
            evPwd.setSelection(str.length()); //因为删除了字符,要重写设置新的光标所在位置
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        
    }
});

使用 XML 属性

在 XML 中使用属性来限制EditText输入字符,如android:inputType和android:digits。

只能输入数字:

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:digits="0123456789" />

只能输入0~9 小写a~z:

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:digits="0123456789abcdefghijklmnopqrstuvwxyz" />

自定义EditText

对于更复杂的需求,可以通过自定义EditText控件实现输入限制。

public class LimitEditText extends EditText {
    public LimitEditText(Context context) {
        super(context);
    }

    public LimitEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LimitEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return new InnerInputConnecttion(super.onCreateInputConnection(outAttrs), false);
    }

    class InnerInputConnecttion extends InputConnectionWrapper implements InputConnection {

        public mInputConnecttion(InputConnection target, boolean mutable) {
            super(target, mutable);
        }

        /**
         * 对输入的内容进行拦截
         *
         * @param text
         * @param newCursorPosition
         * @return
         */
        @Override
        public boolean commitText(CharSequence text, int newCursorPosition) {
            // 只能输入字母或者数字
            if (!Character.isLetterOrDigit(charSequence.charAt(i)) || isChinese)  {
                return false;
            }
            return super.commitText(text, newCursorPosition);
        }

        @Override
        public boolean sendKeyEvent(KeyEvent event) {
            return super.sendKeyEvent(event);
        }

        @Override
        public boolean setSelection(int start, int end) {
            return super.setSelection(start, end);
        }
    }
}

注意事项

  • 当使用TextWatcher时,要小心不要创建无限循环。例如,如果在onTextChanged方法中直接修改EditText的文本,并且没有适当的检查来防止不必要的修改,那么可能会导致无限循环。
  • 对于复杂的输入限制,考虑使用正则表达式来匹配和过滤文本。可以提供更强大和灵活的文本处理能力。

©本文为清一色官方代发,观点仅代表作者本人,与清一色无关。清一色对文中陈述、观点判断保持中立,不对所包含内容的准确性、可靠性或完整性提供任何明示或暗示的保证。本文不作为投资理财建议,请读者仅作参考,并请自行承担全部责任。文中部分文字/图片/视频/音频等来源于网络,如侵犯到著作权人的权利,请与我们联系(微信/QQ:1074760229)。转载请注明出处:清一色财经

(0)
打赏 微信扫码打赏 微信扫码打赏 支付宝扫码打赏 支付宝扫码打赏
清一色的头像清一色管理团队
上一篇 2024年5月29日 00:03
下一篇 2024年5月29日 00:05

相关推荐

发表评论

登录后才能评论

联系我们

在线咨询:1643011589-QQbutton

手机:13798586780

QQ/微信:1074760229

QQ群:551893940

工作时间:工作日9:00-18:00,节假日休息

关注微信