禁止输入特殊字符以及输入法表情,间接保护神一般后台。。。

2017-09-14  本文已影响0人  CarlWu_S
 /**
     * 禁止输入表情以及特殊字符
     */
    public static class EmojiExcludeFilter implements InputFilter {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; i++) {
                int type = Character.getType(source.charAt(i));
                if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
                    return "";
                }
            }
            String speChat = "[`~!@#$%^&*()+=|{}':;'\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘”“’?]";
            Pattern pattern = Pattern.compile(speChat);
            Matcher matcher = pattern.matcher(source.toString());
            if (matcher.find()) {
                return "";
            } else {
                return null;
            }
        }
    }

调用如下:

edtRemark.setFilters(new InputFilter[]{new UIHelper.EmojiExcludeFilter()});

上一篇下一篇

猜你喜欢

热点阅读