美文网首页
Android EditText 限制输入框特殊字符串

Android EditText 限制输入框特殊字符串

作者: DuBetter | 来源:发表于2018-03-12 10:05 被阅读0次

    今天为简单的登录界面的输入框(用户名、密码框,验证码),均为EditText框,做输入限制,不能有空格,不能有一些特殊字符,不多说,直接上代码;

    /**
         * 禁止EditText输入空格
         *
         * @param editText
         */
        public static void setEditTextInhibitInputSpaChat(EditText editText) {
            InputFilter filter_space = new InputFilter() {
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    if (source.equals(" "))
                        return "";
                    else
                        return null;
                }
            };
            InputFilter filter_speChat = new InputFilter() {
                @Override
                public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
                    String speChat = "[`~!@#_$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()— +|{}【】‘;:”“’。,、?]";
                    Pattern pattern = Pattern.compile(speChat);
                    Matcher matcher = pattern.matcher(charSequence.toString());
                    if (matcher.find()) return "";
                    else return null;
                }
            };
            editText.setFilters(new InputFilter[]{filter_space, filter_speChat});
        }
    
    

    相关文章

      网友评论

          本文标题:Android EditText 限制输入框特殊字符串

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