美文网首页
Android Emoji表情过滤

Android Emoji表情过滤

作者: 多思考少说话 | 来源:发表于2017-12-07 16:16 被阅读70次

    Emoji表情编码: https://apps.timwhitlock.info/emoji/tables/unicode
    基本过滤了所有,包括国旗神马的

    public class EmojiFilter implements InputFilter {
        @Override
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            StringBuffer buffer = new StringBuffer();
            for (int i = start; i < end; i++) {
                char codePoint = source.charAt(i);
                if (!getIsEmoji(codePoint)) {
                    buffer.append(codePoint);
                } else {
                    i++;
                }
            }
            if (source instanceof Spanned) {
                SpannableString sp = new SpannableString(buffer);
                TextUtils.copySpansFrom((Spanned) source, start, end, null,
                        sp, 0);
                return sp;
            } else {
                return buffer;
            }
        }
    
        public boolean getIsEmoji(char codePoint) {
            if ((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
                    || (codePoint == 0xD)
                    || ((codePoint >= 0x20) && (codePoint <= 0x29))
                    || ((codePoint >= 0x2A) && (codePoint <= 0x3A))
                    || ((codePoint >= 0x40) && (codePoint <= 0xA8))
                    || ((codePoint >= 0xAF) && (codePoint <= 0x203B))
                    || ((codePoint >= 0x203D) && (codePoint <= 0x2048))
                    || ((codePoint >= 0x2050) && (codePoint <= 0x20e2))
                    || ((codePoint >= 0x20e4) && (codePoint <= 0x2100))
                    || ((codePoint >= 0x21AF) && (codePoint <= 0x2300))
                    || ((codePoint >= 0x23FF) && (codePoint <= 0X24C1))
                    || ((codePoint >= 0X24C3) && (codePoint <= 0x2500))
                    || ((codePoint >= 0x2800) && (codePoint <= 0x2933))
                    || ((codePoint >= 0x2936) && (codePoint <= 0x2AFF))
                    || ((codePoint >= 0x2C00) && (codePoint <= 0x3029))
                    || ((codePoint >= 0x3031) && (codePoint <= 0x303C))
                    || ((codePoint >= 0x303E) && (codePoint <= 0x3296))
                    || ((codePoint >= 0x32A0) && (codePoint <= 0xD7FF))
                    || ((codePoint >= 0xE000) && (codePoint <= 0xFE0E))
                    || ((codePoint >= 0xFE10) && (codePoint <= 0xFFFD))
                    || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) {
                return false;
            }
            return true;
        }
    }
    

    使用:

    editText.setFilters(new InputFilter[]{new EmojiFilter()});
    

    相关文章

      网友评论

          本文标题:Android Emoji表情过滤

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