美文网首页
Android 键盘相关与EditText问题集锦

Android 键盘相关与EditText问题集锦

作者: 唔笛plk | 来源:发表于2018-12-03 09:56 被阅读0次

    一、取消EditText键盘弹出

    1.editText.clearFocus(); // 取消焦点
    2.通过键盘服务管理

         **
         * 隐藏软键盘
         *
         * @param activity
         */
        public static void hideSoftInput(Activity activity) {
            try {
                InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    3.在配置表里,设置Activity

     android:windowSoftInputMode="stateAlwaysHidden"
    

    4.设置EditText属性
    android:focusable="false"

    二、设置EditText数字限制

    **
         * 字数限制提示
         *
         * @param editText
         * @param num
         */
        public static void setLimitNumOfWord(EditText editText, final int num) {
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                }
    
                @Override
                public void afterTextChanged(Editable editable) {
                    if (editable.toString().length() > num) {
                        ToastUtil.showMessage("最多只能输入" + num + "个字符");
                    }
                }
            });
        }
    

    三、EditText相关字符验证

    1.EditText手机号号段校验

    /**
         * * 手机号号段校验,
         * 第1位:1;
         * 第2位:{3、4、5、6、7、8}任意数字;
         * 第3—11位:0—9任意数字
         *
         * @param editText
         * @return
         */
        public static void setPhoneFilter(EditText editText) {
            InputFilter[] inputFilters = new InputFilter[]{(source, start, end, dest, dstart, dend) -> {
                StringBuilder builder = new StringBuilder(dest);
                builder.insert(dstart, source);
                int length = builder.length();
                if (length == 1) {
                    if (builder.charAt(0) == '1') {
                        return source;
                    } else {
                        ToastUtil.showMessage("请输入正确的手机号码");
                        return "";
                    }
                }
    
                if (length > 0 && length <= 11) {
                    int suffixSize = length - 2;
                    Pattern pattern = Pattern.compile("^1[3-9]\\d{" + suffixSize + "}$");
                    if (pattern.matcher(builder.toString()).matches()) {
                        return source;
                    } else {
                        ToastUtil.showMessage("请输入正确的手机号码");
                        return "";
                    }
                }
                return "";
            }};
            editText.setFilters(inputFilters);
        }
    

    2.EditText 合法名字验证

        /**
         * EditText 合法名字验证
         * @param specialNewFilter
         */
        public static void setLegalNameFilter(EditText specialNewFilter) {
            InputFilter[] writeInputFilters = new InputFilter[]{(source, start, end, dest, dstart, dend) -> {
                String speChat = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
                Pattern pattern = Pattern.compile(speChat);
                Matcher matcher = pattern.matcher(source.toString());
                if (matcher.find() || source.equals(" ") || source.toString().contentEquals("\n") || source.equals("-")) {
                    ToastUtil.showMessage("请输入数字、中文、英文");
                    return "";
                } else {
                    return null;
                }
            }};
            specialNewFilter.setFilters(writeInputFilters);
        }
    

    3.过滤换行符

     /**
         * 过滤换行符
         *
         * @param specialNewFilter
         */
        public static void setSpecialFilterEnter(EditText specialNewFilter) {
            InputFilter[] writeInputFilters = new InputFilter[]{(source, start, end, dest, dstart, dend) -> {
                if (source.toString().contentEquals("\n")) {
                    return "";
                } else {
                    return source;
                }
            }};
    
            specialNewFilter.setFilters(writeInputFilters);
        }
    

    4.特殊字符过滤

    **
         * 特殊字符过滤
         *
         * @param speticalFilter
         */
        public static void setSpecialFilter(EditText speticalFilter) {
            InputFilter[] inputFilters = new InputFilter[]{new InputFilter() {
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    String speChat = "[`~!@#$%^&*+=|{}':',\\[\\].<>/?~!@#¥%……&*——+|{}【】‘;:”“’。,、?]";
                    Pattern pattern = Pattern.compile(speChat);
                    Matcher matcher = pattern.matcher(source.toString());
                    if (matcher.find() || source.equals(" ") || source.toString().contentEquals("\n")) {
                        return "";
                    } else {
                        return null;
                    }
                }
            }};
            speticalFilter.setFilters(inputFilters);
        }
    

    5.限定为中文和英语\数字输入

    
        /**
         * 限定为中文和英语\数字输入
         *
         * @param editText
         */
        public static void setNewTextFilter(EditText editText) {
            InputFilter[] inputFilters = new InputFilter[]{
                    (source, start, end, dest, dstart, dend) -> {
                        String s = "[0-9|a-zA-Z|\u4e00-\u9fa5|:”“’。,、?!]+";
                        Pattern pattern = Pattern.compile(s);
                        Matcher m = pattern.matcher(source.toString());
                        if (!m.matches()) {
                            ToastUtil.showMessage("仅能输入中文、英文、数字");
                            return "";
                        }
                        return null;
                    }
            };
            editText.setFilters(inputFilters);
        }
    

    6. 密码字符串过滤

     /**
         * 密码字符串过滤
         *
         * @param editText
         */
        public static void setPwFilter(EditText editText) {
            InputFilter[] inputFilters = new InputFilter[]{(source, start, end, dest, dstart, dend) -> {
                String s = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
                Pattern pattern = Pattern.compile(s);
                Matcher matcher = pattern.matcher(source.toString());
                if (matcher.matches()) {
                    return "";
                } else {
                    return source;
                }
            }};
            editText.setFilters(inputFilters);
        }
    
    

    相关文章

      网友评论

          本文标题:Android 键盘相关与EditText问题集锦

          本文链接:https://www.haomeiwen.com/subject/blhfcqtx.html