EditText限制输入类型为英数字并限制长度
来源:互联网 发布:rayfile for mac下载 编辑:程序博客网 时间:2024/05/21 09:58
方法一:
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());
0 0
- EditText限制输入类型为英数字并限制长度
- Android 限制EditText只能输入数字、限制输入类型、限制输入长度的小技巧
- Android 限制EditText只能输入数字、限制输入类型、限制输入长度的小技巧
- Android 限制EditText只能输入数字、限制输入类型、限制输入长度的小技巧
- Android 限制EditText只能输入数字、限制输入类型、限制输入长度的小技巧
- android edittext 输入长度限制并提示
- android edittext 输入长度限制并提示
- EditText输入长度限制,并提示信息
- EditText限制输入长度
- input只能输入数字并限制长度
- EditText限制输入类型
- android EditText限制输入长度
- Android EditText限制输入长度
- EditText限制输入字符串长度
- 限制输入密码为字母+数字类型
- Android EditText限制文本输入的长度并给予提示
- Android EditText输入限制最大字符长度 和 限制只能输入数字和字母(包含大小写)
- Android EditText 限制输入类型数字或小数,并且小数位数限定为两位
- 【数据压缩】Exp01.彩色空间转换——YUV2RGB
- 瞎谈CNN:通过优化求解输入图像
- COM组件常用接口,以备自用
- 了解身边的超线程、双核、双cpu
- 461. Hamming Distance
- EditText限制输入类型为英数字并限制长度
- windows 远程 Windows命令
- 图片相关
- 抽象类和接口的应用场景
- macbook 下 webstorm 快键键
- 学Javascript的一些感悟
- 有关wince开发的工作小结
- hdu 1536 S-Nim (博弈)
- 判断PC端或移动端跳转至相应页面