允许基于正则表达式的一个EditText唯一入选charcters [英] Allow only selected charcters based on regex in an EditText

查看:167
本文介绍了允许基于正则表达式的一个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 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);
      }
}

这篇关于允许基于正则表达式的一个EditText唯一入选charcters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆