一晚上的工作成果。。。不废话,直接上代码!

layout文件

java文件

public class TextWatcherUtil {    public interface Callback{        void callback();    }    public static void addPhoneNumberTextWatcher(final EditText editText, final Callback callback) {        TextWatcher textWatcher = new TextWatcher() {            private int start = 0;            private int before = 0;            private int count = 0;            @Override            public void beforeTextChanged(CharSequence s, int start, int count, int after) {            }            @Override            public void onTextChanged(CharSequence s, int start, int before, int count) {                this.start = start;                this.before = before;                this.count = count;            }            @Override            public void afterTextChanged(Editable s) {                try {                    filterPhoneNumber(s);                } catch (Exception e) {                }            }            public void filterPhoneNumber(Editable s) throws Exception {                String temp = s.toString();                int selection = editText.getSelectionStart();                char[] arr = temp.toCharArray();                boolean isMatch = true;                if (count > 0) {                    for (int i = start; i < start + count; i++) {                        int c = arr[i];                        if (!(c == 32 || (c >= 48 && c <= 57))) {//不是空格 并且 不是数字                            isMatch = false;                            break;                        }                    }                }                if (isMatch) {                    boolean needReset = false;                    if (arr.length >= 4) {                        if (arr[3] != 32) {                            needReset = true;                        }                    }                    if (!needReset && arr.length >= 9) {                        if (arr[8] != 32) {                            needReset = true;                        }                    }                    if (!needReset) {                        for (int i = 0; i < arr.length; i++) {                            if (arr[i] == 32) {                                if (i != 3 && i != 8) {                                    needReset = true;                                    break;                                }                            }                        }                    }                    if (needReset) {                        temp = temp.replace(" ", "");                        if (count > 0) {                            if (temp.length() >= 7) {                                s.clear();                                s.append(temp.substring(0, 3) + " " + temp.substring(3, 7) + " " + temp.substring(7, temp.length()));                                editText.setSelection(selection - 1);                            } else if (temp.length() >= 3) {                                s.clear();                                s.append(temp.substring(0, 3) + " " + temp.substring(3, temp.length()));                                editText.setSelection(selection - 1);                            }                        } else if (before > 0) {                            if (selection == 3) {                                s.clear();                                if (temp.length() >= 8) {                                    s.append(temp.substring(0, 2) + temp.substring(3, 4) + " " + temp.substring(4, 7) + " " + temp.substring(7, temp.length()));                                } else if (temp.length() >= 4) {                                    s.append(temp.substring(0, 2) + temp.substring(3, 4) + " " + temp.substring(4, temp.length()));                                }                                editText.setSelection(selection - 1);                            } else if (selection == 8) {                                s.clear();                                if (temp.length() >= 8) {                                    s.append(temp.substring(0, 3) + " " + temp.substring(3, 6) + " " + temp.substring(7, temp.length()));                                }                                editText.setSelection(selection - 1);                            } else if (temp.length() >= 7) {                                s.clear();                                s.append(temp.substring(0, 3) + " " + temp.substring(3, 7) + " " + temp.substring(7, temp.length()));                                editText.setSelection(selection);                            } else if (temp.length() >= 3) {                                s.clear();                                s.append(temp.substring(0, 3) + " " + temp.substring(3, temp.length()));                                editText.setSelection(selection);                            }                        }                    } else {                        if (count > 0) {                            if (temp.length() == 3 || temp.length() == 8) {                                s.append(" ");                            }                        } else if (before > 0) {                            if (temp.length() == 3 || temp.length() == 8) {                                s.delete(temp.length() - 1, temp.length());                            }                        }                    }                    if (callback != null)                        callback.callback();                } else {                    s.delete(start, start + count);                }            }        };        editText.addTextChangedListener(textWatcher);    }    public static void addPasswordTextWatcher(final EditText editText, final Callback callback) {        TextWatcher textWatcher = new TextWatcher() {            private int start = 0;            private int count = 0;            @Override            public void beforeTextChanged(CharSequence s, int start, int count, int after) {            }            @Override            public void onTextChanged(CharSequence s, int start, int before, int count) {                this.start = start;                this.count = count;            }            @Override            public void afterTextChanged(Editable s) {                try {                    String temp = s.toString();                    char[] arr = temp.toCharArray();                    boolean isMatch = true;                    if (count > 0) {                        for (int i = start; i < start + count; i++) {                            int c = arr[i];                            if (!((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122))) {//不是数字 && 不是小写字母 && 不是大写字母                                isMatch = false;                                break;                            }                        }                    }                    if (isMatch) {                        if (callback != null)                            callback.callback();                    } else {                        s.delete(start, start + count);                    }                } catch (Exception e) {                }            }        };        editText.addTextChangedListener(textWatcher);    }}

工具类里一个是验证手机号,一个是验证密码

使用方法如下

TextWatcherUtil.addPhoneNumberTextWatcher(phoneNumberEditText, new TextWatcherUtil.Callback() {    @Override    public void callback() {        //todo edittext changed后回调    }});
   
   
   
   
   
                                                                                               

更多相关文章

  1. 基于Android TV端的文件选择器(UI比较丑,主要看逻辑)
  2. Android学习笔记(八)之Android 读写xml文件
  3. Android札记:防反编译、混淆文件p…
  4. Android 身份证号码查询、手机号码查询、天气查询
  5. 总结android中的.gradle文件写法及含义
  6. [Android Studio] 使用本地 aar 文件
  7. Android Studio 实时显示布局文件Preview窗口
  8. Android 学习 之 资源文件 动态修改strings.xml中的字符资源

随机推荐

  1. Android开发手记1--环境配置
  2. Android利器-AndroidStudio快捷键(For Mac
  3. Android之MediaPlayer
  4. android:windowSoftInputMode属性详解
  5. Android(安卓)开发小工具之:Custom Tabs
  6. 【Android】Android(安卓)概述_习题
  7. Android(安卓)UI设计——ListView控件与S
  8. Android键盘使用问题汇总
  9. Android(安卓)学习笔记(一)
  10. android应用安全——数据安全