我希望仅允许用户根据我的 android 应用程序中的正则表达式键入某些字符.如何实现?
I want to allow users only to type certain characters based on the a regex in my android applications. How do I achieve it?
按照@Matt Ball 的建议使用 TextWatcher.
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);
}
}
编辑虽然 TextWatcher 可以工作,但使用 InputFilter 会更简洁.检查此示例.
Edit
Although the TextWatcher works, it would be cleaner to use an InputFilter. Check this example.
这篇关于在 EditText 中仅允许基于正则表达式的选定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在android中剪切、复制、粘贴Cut, copy, paste in android(在android中剪切、复制、粘贴)
android EditText 融入背景android EditText blends into background(android EditText 融入背景)
更改 EditText 的线条颜色 - AndroidChange Line Color of EditText - Android(更改 EditText 的线条颜色 - Android)
EditText 始终显示带 2 位小数的数字EditText showing numbers with 2 decimals at all times(EditText 始终显示带 2 位小数的数字)
更改光标在展开的 EditText 中的开始位置Changing where cursor starts in an expanded EditText(更改光标在展开的 EditText 中的开始位置)
android中的EditText,adjustPan,ScrollView问题EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView问题)