Used a TextWatcher
as @Matt Ball suggested.
@Override
public void afterTextChanged(Editable s) {
String text = s.toString();
int length = text.length();
if(length > 0 && !Pattern.matches(PATTERN, text)) {
s.delete(length - 1, length);
}
}
Edit
Although the TextWatcher
works, it would be cleaner to use an InputFilter
. Check this example.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…