我在我的一个AutoCompleteTextViews中使用上面的属性,但是我也可以输入特殊字符和数字。需要建议将输入类型限制为仅字母,没有数字或特殊字符。
答案 0 :(得分:0)
EditText state = (EditText) findViewById(R.id.txtState);
Pattern ps = Pattern.compile("^[a-zA-Z ]+$");
Matcher ms = ps.matcher(state.getText().toString());
boolean bs = ms.matches();
if (bs == false) {
if (ErrorMessage.contains("invalid"))
ErrorMessage = ErrorMessage + "state,";
else
ErrorMessage = ErrorMessage + "invalid state,";
答案 1 :(得分:0)
试试这个。它完美地为我服务。
EditText editText = (EditText) findViewById(R.id.editText);
editText.setFilters(new InputFilter[]{alphabetFilter, new InputFilter.LengthFilter(10)});
private InputFilter alphabetFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int k = start; k < end; k++) {
if (!Character.isLetter(source.charAt(k))) {
return "";
}
}
return null;
}
};