【Android】android入力ボックスEditText Emoji表情文字の入力禁止

3695 ワード

転載先:http://blog.csdn.net/elsdnwn/article/details/45390771
package com.liujy.ui.wiget;import android.content.Context;import android.text.Editable;import android.text.Selection;import android.text.Spannable;import android.text.TextWatcher;import android.util.AttributeSet;import android.widget.EditText;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 {
                    resetText = false;
                }
            }            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }    /**
     *      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));
    }

}