如何限制 EditText 使用表情符号 [英] How can I restrict EditText to take emojis

查看:45
本文介绍了如何限制 EditText 使用表情符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我希望我的 edittext 应该只使用 Name.我曾尝试使用 android:inputType="textCapSentences" 但它似乎不起作用.我的编辑文本仍在使用表情符号.

I am developing an application where I want my edittext should take Name only. I have tried using android:inputType="textCapSentences" but it seems like its not working. My edittext is still taking the emojis.

那么,有什么办法可以限制在 android.edit 文本中输入的任何特殊字符或表情符号.如果有人有想法,请回复.提前致谢.

So, Is there any way i can restrict any special character or emojis input in edit text in android. If anyone have idea,Please reply. Thanks in advance.

推荐答案

您可以使用表情符号过滤器作为下面的代码

you can use emoji filter as code below

mEditText.setFilters(new InputFilter[]{EMOJI_FILTER});

public static InputFilter EMOJI_FILTER = new InputFilter() {
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        boolean keepOriginal = true;
        StringBuilder sb = new StringBuilder(end - start);
        for (int index = start; index < end; index++) {
            int type = Character.getType(source.charAt(index));
            if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
                return "";
            }
            char c = source.charAt(index);
            if (isCharAllowed(c)) 
                sb.append(c);
            else
                keepOriginal = false;
        }
        if (keepOriginal)
            return null;
        else {
            if (source instanceof Spanned) {
                SpannableString sp = new SpannableString(sb);
                TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, sp, 0);
                return sp;
            } else {
                return sb;
            }
        }
    }
};

private static boolean isCharAllowed(char c) {
    return Character.isLetterOrDigit(c) || Character.isSpaceChar(c);
}

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

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