有两种方案:
1.在xml:EditText 设置属性----
android:digis="ABCDE555555&&&&&"
ABCDE555555&&&&&"是你的限制规则。
例如:android:digits="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
规则是只能输入英文字母(小写)和数字
2.EditText,TextView只能输入字母加数字,可在View空间后面加上监听器,如下
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<=122){//小写字母
return;
}
edt.delete(temp.length()-1, temp.length());
} catch (Exception e) {
// TODO: handle exception
}
}
});
1.在xml:EditText 设置属性----
android:digis="ABCDE555555&&&&&"
ABCDE555555&&&&&"是你的限制规则。
例如:android:digits="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
规则是只能输入英文字母(小写)和数字
2.EditText,TextView只能输入字母加数字,可在View空间后面加上监听器,如下
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<=122){//小写字母
return;
}
edt.delete(temp.length()-1, temp.length());
} catch (Exception e) {
// TODO: handle exception
}
}
});