如何在Android中的EditText中检测表情符号

问题描述:

我想检测我的EditText是否包含笑脸(表情符号).但是我不知道如何检测它们.

I want to detect whether my EditText contains smilie (emoticons) or not. But I have no idea that how to detect them.

要在键盘上打字时禁用表情符号,我使用以下过滤器:

To disable emoji characters when typing on the keyboard I using the following filter:

InputFilter filter = new 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));
            //System.out.println("Type : " + type);
            if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
                return "";
            }
        }
        return null;
    }
};

mMessageEditText.setFilters(new InputFilter[]{filter});

如果仅需要检测EditText是否包含任何表情符号字符,则可以在android.text.TextWatcher接口实现中(在onTextChange()afterTextChanged()方法中)或例如使用此原理(Character.getType()).使用charAt()方法在mMessageEditText.getText()上使用简单的for循环(返回CharSequence类).

If you need only detect if EditText contains any emoji character you can use this priciple (Character.getType()) in android.text.TextWatcher interface implementation (in onTextChange() or afterTextChanged() method) or e.g. use simple for cycle on mMessageEditText.getText() (returns CharSequence class) with charAt() method.