사용자 입력의 제어를 위해 사용 InputFilter
:
여기
EditText editText;
editText.setFilters(new InputFilter[] { new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
// here you can evaluate user input if it's correct or not
}
} });
는 filter
메소드 구현 가능하지만,이 테스트되지 않습니다
if (source.length() == 0) {
return null;//deleting, keep original editing
}
String result = "";
result.concat(dest.toString().substring(0, dstart));
result.concat(source.toString().substring(start, end));
result.concat(dest.toString().substring(dend, dest.length()));
if (result.length() > 5) {
return "";// do not allow this edit
}
boolean allowEdit = true;
char c;
if (result.length() > 0) {
c = result.charAt(0);
allowEdit &= (c >= '0' && c <= '2');
}
if (result.length() > 1) {
c = result.charAt(1);
allowEdit &= (c >= '0' && c <= '9');
}
if (result.length() > 2) {
c = result.charAt(2);
allowEdit &= (c == ':');
}
if (result.length() > 3) {
c = result.charAt(3);
allowEdit &= (c >= '0' && c <= '5');
}
if (result.length() > 4) {
c = result.charAt(4);
allowEdit &= (c >= '0' && c <= '9');
}
return allowEdit ? null : "";
어떻게 5 자리로 글고 제한에 대해? – Thommy
2 개의 EditText가 각각에 대한 제한을 선언하고 사후 처리에서 값을 결합합니다. 이렇게하면':'을 삽입하고 사용자가 키보드에서 그것을 찾지 못하게 할 수 있습니까? – jnthnjns
은 http://developer.android.com/reference/java/util/regex/Pattern.html 일 수 있습니다. –