如何禁止在Android EditText中输入表情符号? [英] How to disable emoji from being entered in Android EditText?

查看:769
本文介绍了如何禁止在Android EditText中输入表情符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EditText和TextView的text inputType的大多数实现(URI,密码等除外)都允许Emoji表情-尽管在大多数Google键盘配置中,此按钮是隐藏的.有没有一种方法可以禁止将Emoji表情输入到EditText中?是否有可以与textMultiLine配对的inputType参数可以禁用表情符号?

Most implementations of the text inputType (other than URI, password, etc.) for EditText and TextView allow Emoji - although in most Google keyboard configurations this button is hidden. Is there a way to disable Emoji from being entered in an EditText? Is there an inputType parameter that could be paired with textMultiLine that would disable Emoji?

推荐答案

修改 build.gradle 文件,添加 XEditText 到您的项目:

Modify build.gradle file, add XEditText to your project:

dependencies{
    compile 'com.xw.repo:xedittext:2.0.0@aar'
}

然后,在您的 layout.xml 中:

<com.xw.repo.XEditText
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:x_disableEmoji="true"/>

或:

像这样自定义EditText:

Or:

Customize EditText like this:

public class CustomEditText extends EditText {
    public CustomEditText(Context context) {
        super(context);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setFilters(new InputFilter[]{new EmojiExcludeFilter()});
    }

    private class EmojiExcludeFilter implements 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));
                if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
                    return "";
                }
            }
            return null;
        }
    }
}

两个都很好!

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

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