EditView不支持輸入特殊字符以及emoji表情

項目中的輸入框因爲android與ios10中的emoji表情不對等導致emoji表情不顯示問題,要求屏蔽掉emoji表情。在此同時加入了判斷是否輸入特殊字符限制。

public class ContainsEmojiEditText extends EditText {
    //輸入表情前的光標位置
    private int cursorPos;
    //輸入表情前EditText中的文本
    private String inputAfterText;
    //是否重置了EditText的內容
    private boolean resetText;

    private Context mContext;

    public ContainsEmojiEditText(Context context) {
        super(context);
        this.mContext = context;
        initEditText();
    }

    public ContainsEmojiEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initEditText();
    }

    public ContainsEmojiEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initEditText();
    }

    // 初始化edittext 控件
    private void initEditText() {
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int before, int count) {
                if (!resetText) {
                    cursorPos = getSelectionEnd();
                    // 這裏用s.toString()而不直接用s是因爲如果用s,
                    // 那麼,inputAfterText和s在內存中指向的是同一個地址,s改變了,
                    // inputAfterText也就改變了,那麼表情過濾就失敗了
                    inputAfterText = s.toString();
                }

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!resetText) {
                    if (count >= 2) {//表情符號的字符長度最小爲2
                        CharSequence input = s.subSequence(cursorPos, cursorPos + count);
                        if (containsEmoji(input.toString())) {
                            resetText = true;
                            Toast.makeText(mContext, "不支持輸入Emoji表情符號", Toast.LENGTH_SHORT).show();
                            //是表情符號就將文本還原爲輸入表情符號之前的內容
                            setText(inputAfterText);
                            CharSequence text = getText();
                            if (text instanceof Spannable) {
                                Spannable spanText = (Spannable) text;
                                Selection.setSelection(spanText, text.length());
                            }
                        }
                    } else if (count == 1) {
                        CharSequence input = s.subSequence(cursorPos, cursorPos + count);
                        if (containsUnChar(input.toString())) {
                            resetText = true;
                            Toast.makeText(mContext, "不支持輸入特殊符號", Toast.LENGTH_SHORT).show();
                            //是表情符號就將文本還原爲輸入表情符號之前的內容
                            setText(inputAfterText);
                            CharSequence text = getText();
                            if (text instanceof Spannable) {
                                Spannable spanText = (Spannable) text;
                                Selection.setSelection(spanText, text.length());
                            }
                        }

                    }
                } else {
                    resetText = false;
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    /**
     * 檢查是否輸入了特殊字符,若後期需要屏蔽新字符可直接添加
     * @param tag
     * @return
     */
    private boolean containsUnChar(String tag) {
        boolean isOk = false;
        String regex = "[-_——`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?\\s]";
        /*if (!TextUtils.isEmpty(tag)) {
            int length = tag.length();
            for (int i = 0; i < length; i++) {
                String ch = tag.substring(i, i + 1);
                if (ch.matches(regex)) {
                    isOk = true;
                    break;
                }
            }
        }*/
        if (tag.matches(regex)) {
            isOk = true;
        }
        return isOk;
    }


    /**
     * 檢測是否有emoji表情
     *
     * @param source
     * @return
     */
    public static boolean containsEmoji(String source) {
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (!isEmojiCharacter(codePoint)) { //如果不能匹配,則該字符是Emoji表情
                return true;
            }
        }
        return false;
    }

    /**
     * 判斷是否是Emoji
     *
     * @param codePoint 比較的單個字符
     * @return
     */
    private static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) ||
                (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000)
                && (codePoint <= 0x10FFFF));
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章