Daiwang转 Android 巧用正则表达式+TextWatcher



Android 巧用正则表达式+TextWatcher实时限制用户输入

一般我们在限制用户输入的时候,都是要等到用户在EditText里面输入了文本后,点击某个按钮,再去校验用户的输入是否符合规范。

接下来我介绍个新方法,可以在用户输入不规范的字符后,还没在EditText里显示出来时,就把用户的错误输入给忽略掉,不显示在EditText中,只有当用户输入了符合规范的字符时才能在EditText中显示出来。

这里需要使用到正则表达式和TextWatcher接口。

对TextWatcher不是很了解的同学看我的这篇博客:http://blog.csdn.net/zhuwentao2150/article/details/51546773

主要思路

当用户输入时,使用TextWatcher对用户的输入进行抓取,在文本还没有显示到界面之前,使用正则表达式作为判定条件,判断用户的输入是否符合标准,不符合则不显示在EditText中。

具体实现

ExamineTextWatcher实现了TextWatcher接口,在里面对用户输入的字符进行抓取,并调用校验方法

[java]  view plain  copy
  1. /** 
  2. * 用户输入验证器 
  3. * <p/> 
  4. * Created by zhuwentao on 2016-08-04. 
  5. */  
  6. public class ExamineTextWatcher implements TextWatcher {  
  7.     private static final String TAG = "ExamineTextWatcher";  
  8.   
  9.     /** 
  10.      * 帐号 
  11.      */  
  12.     public static final int TYPE_ACCOUNT = 1;  
  13.   
  14.     /** 
  15.      * 金额 
  16.      */  
  17.     public static final int TYPE_MONEY = 2;  
  18.   
  19.     /** 
  20.      * 输入框 
  21.      */  
  22.     private EditText mEditText;  
  23.   
  24.     /** 
  25.      * 验证类型 
  26.      */  
  27.     private int examineType;  
  28.   
  29.     /** 
  30.      * 输入前的文本内容 
  31.      */  
  32.     private String beforeText;  
  33.   
  34.     /** 
  35.      * 构造器 
  36.      * 
  37.      * @param type     验证类型 
  38.      * @param editText 输入框 
  39.      */  
  40.     public ExamineTextWatcher(int type, EditText editText) {  
  41.         this.examineType = type;  
  42.         this.mEditText = editText;  
  43.     }  
  44.   
  45.     @Override  
  46.     public void beforeTextChanged(CharSequence s, int start, int count, int after) {  
  47.         // 输入前的字符  
  48.         beforeText = s.toString();  
  49.         Log.d(TAG, "beforeText =>>>" + beforeText );  
  50.     }  
  51.   
  52.     @Override  
  53.     public void onTextChanged(CharSequence s, int start, int before, int count) {  
  54.         // 输入后的字符  
  55.         String afterText = s.toString();  
  56.         Log.d(TAG, "afterText =>>>" + afterText);  
  57.   
  58.         boolean isValid = true;  
  59.         if (!TextUtils.isEmpty(afterText)) {  
  60.             switch (examineType) {  
  61.                 case TYPE_ACCOUNT:  
  62.                     isValid = ValidateUtil.isAccount(afterText);  
  63.                     break;  
  64.                 case TYPE_MONEY:  
  65.                     isValid = ValidateUtil.isMoney(afterText);  
  66.                     break;  
  67.             }  
  68.             if (!isValid) {  
  69.                 // 用户现在输入的字符数减去之前输入的字符数,等于新增的字符数  
  70.                 int differ = afterText.length() - beforeText.length();  
  71.                 // 如果用户的输入不符合规范,则显示之前输入的文本  
  72.                 mEditText.setText(beforeText);  
  73.                 // 光标移动到文本末尾  
  74.                 mEditText.setSelection(afterText.length() - differ);  
  75.             }  
  76.         }  
  77.     }  
  78.   
  79.     @Override  
  80.     public void afterTextChanged(Editable s) {  
  81.   
  82.     }  
  83. }  

新建一个工具类ValidateUtil,使用正则表达式对用户输入的字符进行匹配

[java]  view plain  copy
  1. /** 
  2. * 验证工具 
  3. * 
  4. * Created by zhuwentao on 2016-08-04. 
  5. */  
  6. public class ValidateUtil {  
  7.   
  8.     private ValidateUtil() {  
  9.         // 防止被实例化  
  10.     }  
  11.   
  12.     /** 
  13.      * 字符串是否符合正则表达式的规则 
  14.      * 
  15.      * @param text 匹配文本 
  16.      * @param format 匹配规则 
  17.      * @return true 匹配成功 flase 匹配失败 
  18.      */  
  19.     private static boolean isMatches(String text, String format) {  
  20.         Pattern pattern = Pattern.compile(format);  
  21.         Matcher m = pattern.matcher(text);  
  22.         return m.matches();  
  23.     }  
  24.   
  25.     /** 
  26.      * 匹配帐号类型是否正确(只能输入大小写字母和数字,最大不超过20个字符) 
  27.      * 
  28.      * @param str 帐号 
  29.      * @return true= 符合 false=不符合 
  30.      */  
  31.     public static boolean isAccount(String str) {  
  32.         String format = "[a-zA-Z0-9]{0,20}";  
  33.         return isMatches(str, format);  
  34.     }  
  35.   
  36.     /** 
  37.      * 匹配金额是否符合要求(99999999.99) 
  38.      * 
  39.      * @param money 金额字符串 
  40.      * @return true= 符合 false=不符合 
  41.      */  
  42.     public static boolean isMoney(String money) {  
  43.         String regex = "(^[1-9][0-9]{0,7}(\\.[0-9]{0,2})?)|(^0(\\.[0-9]{0,2})?)";  
  44.         return isMatches(money, regex);  
  45.     }  
  46. }  

通过以上两个类就可以对用户的输入进行实时限制了。

使用方法很简单,给需要限制的EditText添加addTextChangedListener()方法,传入ExamineTextWatcher对象

[java]  view plain  copy
  1. mEditText.addTextChangedListener(new ExamineTextWatcher(ExamineTextWatcher.TYPE_ACCOUNT, mEditText));  

需要添加其它限制条件时,在ValidateUtil类里添加限制条件,然后在ExamineTextWatcher类里配置好相应的验证类型标识,最后在onTextChanged()方法中调用对应的验证条件


Demo源码:传送门


Android 巧用正则表达式+TextWatcher实时限制用户输入

一般我们在限制用户输入的时候,都是要等到用户在EditText里面输入了文本后,点击某个按钮,再去校验用户的输入是否符合规范。

接下来我介绍个新方法,可以在用户输入不规范的字符后,还没在EditText里显示出来时,就把用户的错误输入给忽略掉,不显示在EditText中,只有当用户输入了符合规范的字符时才能在EditText中显示出来。

这里需要使用到正则表达式和TextWatcher接口。

对TextWatcher不是很了解的同学看我的这篇博客:http://blog.csdn.net/zhuwentao2150/article/details/51546773

主要思路

当用户输入时,使用TextWatcher对用户的输入进行抓取,在文本还没有显示到界面之前,使用正则表达式作为判定条件,判断用户的输入是否符合标准,不符合则不显示在EditText中。

具体实现

ExamineTextWatcher实现了TextWatcher接口,在里面对用户输入的字符进行抓取,并调用校验方法

[java]  view plain  copy
  1. /** 
  2. * 用户输入验证器 
  3. * <p/> 
  4. * Created by zhuwentao on 2016-08-04. 
  5. */  
  6. public class ExamineTextWatcher implements TextWatcher {  
  7.     private static final String TAG = "ExamineTextWatcher";  
  8.   
  9.     /** 
  10.      * 帐号 
  11.      */  
  12.     public static final int TYPE_ACCOUNT = 1;  
  13.   
  14.     /** 
  15.      * 金额 
  16.      */  
  17.     public static final int TYPE_MONEY = 2;  
  18.   
  19.     /** 
  20.      * 输入框 
  21.      */  
  22.     private EditText mEditText;  
  23.   
  24.     /** 
  25.      * 验证类型 
  26.      */  
  27.     private int examineType;  
  28.   
  29.     /** 
  30.      * 输入前的文本内容 
  31.      */  
  32.     private String beforeText;  
  33.   
  34.     /** 
  35.      * 构造器 
  36.      * 
  37.      * @param type     验证类型 
  38.      * @param editText 输入框 
  39.      */  
  40.     public ExamineTextWatcher(int type, EditText editText) {  
  41.         this.examineType = type;  
  42.         this.mEditText = editText;  
  43.     }  
  44.   
  45.     @Override  
  46.     public void beforeTextChanged(CharSequence s, int start, int count, int after) {  
  47.         // 输入前的字符  
  48.         beforeText = s.toString();  
  49.         Log.d(TAG, "beforeText =>>>" + beforeText );  
  50.     }  
  51.   
  52.     @Override  
  53.     public void onTextChanged(CharSequence s, int start, int before, int count) {  
  54.         // 输入后的字符  
  55.         String afterText = s.toString();  
  56.         Log.d(TAG, "afterText =>>>" + afterText);  
  57.   
  58.         boolean isValid = true;  
  59.         if (!TextUtils.isEmpty(afterText)) {  
  60.             switch (examineType) {  
  61.                 case TYPE_ACCOUNT:  
  62.                     isValid = ValidateUtil.isAccount(afterText);  
  63.                     break;  
  64.                 case TYPE_MONEY:  
  65.                     isValid = ValidateUtil.isMoney(afterText);  
  66.                     break;  
  67.             }  
  68.             if (!isValid) {  
  69.                 // 用户现在输入的字符数减去之前输入的字符数,等于新增的字符数  
  70.                 int differ = afterText.length() - beforeText.length();  
  71.                 // 如果用户的输入不符合规范,则显示之前输入的文本  
  72.                 mEditText.setText(beforeText);  
  73.                 // 光标移动到文本末尾  
  74.                 mEditText.setSelection(afterText.length() - differ);  
  75.             }  
  76.         }  
  77.     }  
  78.   
  79.     @Override  
  80.     public void afterTextChanged(Editable s) {  
  81.   
  82.     }  
  83. }  

新建一个工具类ValidateUtil,使用正则表达式对用户输入的字符进行匹配

[java]  view plain  copy
  1. /** 
  2. * 验证工具 
  3. * 
  4. * Created by zhuwentao on 2016-08-04. 
  5. */  
  6. public class ValidateUtil {  
  7.   
  8.     private ValidateUtil() {  
  9.         // 防止被实例化  
  10.     }  
  11.   
  12.     /** 
  13.      * 字符串是否符合正则表达式的规则 
  14.      * 
  15.      * @param text 匹配文本 
  16.      * @param format 匹配规则 
  17.      * @return true 匹配成功 flase 匹配失败 
  18.      */  
  19.     private static boolean isMatches(String text, String format) {  
  20.         Pattern pattern = Pattern.compile(format);  
  21.         Matcher m = pattern.matcher(text);  
  22.         return m.matches();  
  23.     }  
  24.   
  25.     /** 
  26.      * 匹配帐号类型是否正确(只能输入大小写字母和数字,最大不超过20个字符) 
  27.      * 
  28.      * @param str 帐号 
  29.      * @return true= 符合 false=不符合 
  30.      */  
  31.     public static boolean isAccount(String str) {  
  32.         String format = "[a-zA-Z0-9]{0,20}";  
  33.         return isMatches(str, format);  
  34.     }  
  35.   
  36.     /** 
  37.      * 匹配金额是否符合要求(99999999.99) 
  38.      * 
  39.      * @param money 金额字符串 
  40.      * @return true= 符合 false=不符合 
  41.      */  
  42.     public static boolean isMoney(String money) {  
  43.         String regex = "(^[1-9][0-9]{0,7}(\\.[0-9]{0,2})?)|(^0(\\.[0-9]{0,2})?)";  
  44.         return isMatches(money, regex);  
  45.     }  
  46. }  

通过以上两个类就可以对用户的输入进行实时限制了。

使用方法很简单,给需要限制的EditText添加addTextChangedListener()方法,传入ExamineTextWatcher对象

[java]  view plain  copy
  1. mEditText.addTextChangedListener(new ExamineTextWatcher(ExamineTextWatcher.TYPE_ACCOUNT, mEditText));  

需要添加其它限制条件时,在ValidateUtil类里添加限制条件,然后在ExamineTextWatcher类里配置好相应的验证类型标识,最后在onTextChanged()方法中调用对应的验证条件


Demo源码:传送门


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值