Android限定EditText的输入类型为数字或者英文(包括大小写)// 监听密码输入框的输入内容类型,不可以输入中文 TextWatcher mTextWatcher = new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void afterTextChanged(Editable edt) {String temp = edt.toString();if(edt.toString().getBytes().length != edt.length()){edt.delete(temp.length()-1, temp.length());}//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) {//try {//throw new Exception("登录页面监听密码输入框只能输入数字或者英文出错");//} catch (Exception e1) {//e1.printStackTrace();//}//}}};其实有两种方案:
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;
}<