终于要写博客了,以前总觉得技术大牛才能写博客,后面想想,分享个人想法与技术探讨也是一种不错的交流方式。今天讲讲我在项目中遇到的一个实际需求,也许很多人也遇到过:
EditText要求限制输入数字和英文,但是最好默认弹出数字键盘。
我也上网搜过很多种方式,将自己的总结经验带给大家参考:
方法一 在xml里面设置 android:digits
android:digits="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
结果:可以实现限制输入数字和英文的效果,但是默认弹出的全键盘而不是数字键盘。
方法二 为EditText添加监听器
tvPassword.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable edt) {
try {
String temp = edt.toString();
String tem = temp.substring(temp.length() - 1, temp.length());
char[] temC = tem.toCharArray();
int mid = temC[0];
if (mid >= 48 && mid <= 57) {//数字
return;
}
if (mid >= 65 && mid <= 90) {//大写字母
return;
}
if (mid > 97 && mid <=