EditText限制输入

需求是奇怪的,干活的 遇到需求是无力回天的;这不

//只能输入汉字(正则)

public static String setEditTextFilter(String str) throws PatternSyntaxException {
    String mEditString= "[^a-zA-Z\u4E00-\u9FA5]";
    Pattern mPattern = Pattern.compile(mEditString);
    Matcher mMatcher = mPattern .matcher(str);
    return mPattern .replaceAll("").trim();
}

// 禁止EditText输入特殊字符

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

也可以使用代码设置setInputType()方法

Xml里面通过digits来限制"只能输入"
1.android:inputType
2.android:digits=“0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”

你可能感兴趣的:(android日记)