常用InputFilter

[toc]

长度限制InputFilter:

 public static InputFilter getInputFilter(int length) {
        //限制问题内容输入字符长度
        InputFilter inputFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                try {
                    //转换成中文字符集的长度
                    int destLen = dest.toString().getBytes("GB18030").length;
                    int sourceLen = source.toString().getBytes("GB18030").length;
                    Log.i("tag", "String.valueOf(destLen + sourceLen)=" + String.valueOf(destLen + sourceLen));
                    //如果超过100个字符
                    if (destLen + sourceLen > length) {
                        String str = subStringByBytes(source.toString(), length - destLen);
                        Log.i("getInputFilter", "str:" + str);
                        return str;
                    }
                    //如果按返回键
                    if (source.length() < 1 && (dend - dstart >= 1)) {
                        return dest.subSequence(dstart, dend - 1);
                    }
                    //其他情况直接返回输入的内容
                    return source;
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return "";
            }
        };
        return inputFilter;
    }

过滤emoji (包括输入法中带的emoji)

 public static InputFilter getEmojiFilter() {
        //限制问题内容输入字符长度
        InputFilter inputFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

                try {
                    // 过滤emoji
                    Pattern p2 = Pattern.compile("(?:[\uD83C\uDF00-\uD83D\uDDFF]|[\uD83E\uDD00-\uD83E\uDDFF]|[\uD83D\uDE00-\uD83D\uDE4F]|[\uD83D\uDE80-\uD83D\uDEFF]|[\u2600-\u26FF]\uFE0F?|[\u2700-\u27BF]\uFE0F?|\u24C2\uFE0F?|[\uD83C\uDDE6-\uD83C\uDDFF]{1,2}|[\uD83C\uDD70\uD83C\uDD71\uD83C\uDD7E\uD83C\uDD7F\uD83C\uDD8E\uD83C\uDD91-\uD83C\uDD9A]\uFE0F?|[\u0023\u002A\u0030-\u0039]\uFE0F?\u20E3|[\u2194-\u2199\u21A9-\u21AA]\uFE0F?|[\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55]\uFE0F?|[\u2934\u2935]\uFE0F?|[\u3030\u303D]\uFE0F?|[\u3297\u3299]\uFE0F?|[\uD83C\uDE01\uD83C\uDE02\uD83C\uDE1A\uD83C\uDE2F\uD83C\uDE32-\uD83C\uDE3A\uD83C\uDE50\uD83C\uDE51]\uFE0F?|[\u203C\u2049]\uFE0F?|[\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE]\uFE0F?|[\u00A9\u00AE]\uFE0F?|[\u2122\u2139]\uFE0F?|\uD83C\uDC04\uFE0F?|\uD83C\uDCCF\uFE0F?|[\u231A\u231B\u2328\u23CF\u23E9-\u23F3\u23F8-\u23FA]\uFE0F?)");
                    Matcher m2 = p2.matcher(source);
                    boolean matches = m2.matches();
                    if (matches) {
                        return m2.replaceAll("").trim();
                    }
                    return source;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return source;
            }
        };
        return inputFilter;
    }

你可能感兴趣的:(常用InputFilter)