Android禁止EditText输入特殊字符

限制EditText输入的字符类型, 如空格,特殊字符等:

 public static void setEditTextInhibitInputSpeChat(EditText editText){
          InputFilter filter=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())return "";
                else return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
}

你可能感兴趣的:(Android学习,android,webview,java)