EditText禁止输入中文或禁止emoji或只能输入英文和数字

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cui130/article/details/80845127

禁止输入中文:

edit.setKeyListener(new DigitsKeyListener() {
    @Override
    public int getInputType() {
        return InputType.TYPE_TEXT_VARIATION_PASSWORD;
    }

    @Override
    protected char[] getAcceptedChars() {
        String dataID = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`¬!\"£$%^*()~=#{}[];':,./?/*-_+<>@&#038";
        char[] data = dataID.toCharArray();
        return data;
    }
});

禁止输入emoji:

InputFilter emojiFilter = new InputFilter() {
    Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
            Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        Matcher emojiMatcher = emoji.matcher(source);
        if (emojiMatcher.find()) {
            return "";
        }
        return null;
    }
};
edit.setFilters(new InputFilter[]{emojiFilter});

只能输入英文和数字:

edit.setKeyListener(new DigitsKeyListener() {
    @Override
    public int getInputType() {
        return InputType.TYPE_TEXT_VARIATION_PASSWORD;
    }

    @Override
    protected char[] getAcceptedChars() {
        String dataID = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM1234567890";
        char[] data = dataID.toCharArray();
        return data;
    }
});

猜你喜欢

转载自blog.csdn.net/cui130/article/details/80845127
今日推荐