如何在Android中的EditText中检测表情符号 [英] How to detect emoticons in EditText in android

查看:366
本文介绍了如何在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.

这篇关于如何在Android中的EditText中检测表情符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆