如何在允许表情符号输入的情况下限制edittext中的最大字符? [英] how to limit max characters in edittext with emoji inputs allowed?

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

问题描述

我有一个需求,我需要限制在EditText中输入的字符数.我知道可以使用属性 android:maxLength 表示我的.xml布局文件中的EditText.

I have a requirement where I need to limit the number of characters entered in EditText. I know this can be easily achieved with the attribute android:maxLength for my EditText in my .xml layout file.

但是我的问题是我的EditText还应该允许用户输入表情符号.现在的问题是,某些表情符号的长度有时为2或有时为1.因此, android:maxLength = 1 不允许输入长度= 2的表情符号.

But my problem is my EditText should also allow users to enter emojis. Now the catch is, the length of some of the emojis is sometimes 2 or sometimes 1. So, android:maxLength=1 doesn't allow entering emojis with length = 2.

使用Character类的这种方法,我可以获得正确的字符串长度(每个表情符号字符都计为1):

I can get the correct length of a string (with each emoji character counted as 1) with this method of Character class:

Character.codePointCount(charSequence.toString(), 0, charSequence.toString().length())

我尝试像这样使用InputFilter:

I tried using InputFilter like so:

InputFilter inputFilter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
                if (Character.codePointCount(charSequence.toString(), 0, charSequence.toString().length()) <= maxCharactersAllowed) {
                    return null;
                } else {
                    return "";
                }
            }
        };

但是charSequence返回给我纯文本和表情符号文本的奇怪结果,所以我用来比较长度的字符串给出了奇怪的结果.

But charSequence returned gives me weird results for plaintext and emoji text, so that the string I am using for comparison of length gives out weird results.

有人可以帮我正确实现对EditText接受表情符号字符的最大字符数的限制吗?

Can someone please help me to correctly implement restriction of the maximum number of characters for EditText accepting emoji characters as well?

推荐答案

谢谢,.在您的帮助下,我找到了这样的解决方案:

Thanks, Joe. With your help I was able to find a solution like so:

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                oldTextString = charSequence.toString();
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String newTextString = editable.toString();
                if (!oldTextString.equals(newTextString)) {
                    if (Character.codePointCount(newTextString, 0, newTextString.length()) > maxCharactersAllowed) {
                        newTextString = oldTextString;
                    }
                    editText.setText(newTextString);
                    editText.setSelection(newTextString.length());
                }
            }
        });

这篇关于如何在允许表情符号输入的情况下限制edittext中的最大字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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