允许基于正则表达式的一个EditText唯一入选charcters正则表达式、EditText、charcters

由网友(- 多情的男人未必花心)分享简介:我想只允许用户输入的基础上在我的Andr​​oid应用程序的正则表达式某些字符。我该如何实现呢?I want to allow users only to type certain characters based on the a regex in my android applications. How do I...

我想只允许用户输入的基础上在我的Andr​​oid应用程序的正则表达式某些字符。我该如何实现呢?

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);
      }
}
阅读全文

相关推荐

最新文章