由网友(- 多情的男人未必花心)分享简介:我想只允许用户输入的基础上在我的Android应用程序的正则表达式某些字符。我该如何实现呢?I want to allow users only to type certain characters based on the a regex in my android applications. How do I...
我想只允许用户输入的基础上在我的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?
推荐答案
用于一个 TextWatcher
作为@马特球建议。
Used a TextWatcher
as @Matt Ball suggested.
@Override
public void afterTextChanged(Editable s) {
String text = s.toString();
int length = text.length();
if(!Pattern.matches(PATTERN, text)) {
s.delete(length - 1, length);
}
}
相关推荐
最新文章