EditText限制输入类型为英数字并限制长度

方法一:

EditText标签下增加以下属性:

android:maxLength="18"
android:digits="1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"

方法二:

通过TextWatcher实现,更加灵活。

首先,自定义一个TextWatcher:

public static class IDNumberTextWatcher implements android.text.TextWatcher {
        /**
         * This method is called to notify you that, within <code>s</code>,
         * the <code>count</code> characters beginning at <code>start</code>
         * are about to be replaced by new text with length <code>after</code>.
         * It is an error to attempt to make changes to <code>s</code> from
         * this callback.
         */
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }
        /**
         * This method is called to notify you that, within <code>s</code>,
         * the <code>count</code> characters beginning at <code>start</code>
         * have just replaced old text that had length <code>before</code>.
         * It is an error to attempt to make changes to <code>s</code> from
         * this callback.
         */
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            
        }
        /**
         * This method is called to notify you that, somewhere within
         * <code>s</code>, the text has been changed.
         * It is legitimate to make further changes to <code>s</code> from
         * this callback, but be careful not to get yourself into an infinite
         * loop, because any changes you make will cause this method to be
         * called again recursively.
         * (You are not told where the change took place because other
         * afterTextChanged() methods may already have made other changes
         * and invalidated the offsets.  But if you need to know here,
         * you can use {@link Spannable#setSpan} in {@link #onTextChanged}
         * to mark your place and then look up from here where the span
         * ended up.
         */
        @Override
        public void afterTextChanged(Editable s) {
            String temp = s.toString();
            Log.d(TAG, "temp=" + temp);
            if (!TextUtils.isEmpty(temp)) {
                if (temp.length() <= 18) {
                    String tem = temp.substring(temp.length() - 1, temp.length());
                    char[] temC = tem.toCharArray();
                    int mid = temC[0];
                    // Number
                    if(mid>=48&&mid<=57){
                        return;
                    }
                    // Upper case
                    if(mid>=65&&mid<=90){
                        return;
                    }
                    // Lower case
                    if(mid>=97&&mid<=122){
                        return;
                    }
                }
                else {
		    Log.d(TAG, "current input length is over 18");
 		}
 		s.delete(temp.length()-1, temp.length());
 	    }
 		else {
 		Log.d(TAG, "current input is null");
 	    }
 	}
}
然后在EditText的对象上设置该TextWatcher即可:

idNumEt.addTextChangedListener(new IDNumberTextWatcher());




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未子涵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值