今天为简单的登录界面的输入框(用户名、密码框,验证码),均为EditText框,做输入限制,不能有空格,不能有一些特殊字符,不多说,直接上代码;
/**
* 禁止EditText输入空格
*
* @param editText
*/
public static void setEditTextInhibitInputSpaChat(EditText editText) {
InputFilter filter_space = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source.equals(" "))
return "";
else
return null;
}
};
InputFilter filter_speChat = new InputFilter() {
@Override
public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
String speChat = "[`~!@#_$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()— +|{}【】‘;:”“’。,、?]";
Pattern pattern = Pattern.compile(speChat);
Matcher matcher = pattern.matcher(charSequence.toString());
if (matcher.find()) return "";
else return null;
}
};
editText.setFilters(new InputFilter[]{filter_space, filter_speChat});
}
网友评论