首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置长度字符Emoji编辑文本Android

设置长度字符Emoji编辑文本Android
EN

Stack Overflow用户
提问于 2022-04-27 14:28:27
回答 2查看 379关注 0票数 0

我有一个编辑文本maxLength = 30,但我只能键入6个字符=> 1表情符号狗=6个规则字符。所以请帮我键入30个表情符号。谢谢大家。

在这里输入图像描述

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-28 08:41:43

代码语言:javascript
复制
editText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                oldTextString = charSequence.toString()
            }

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun afterTextChanged(editable: Editable) {
                var newTextString = editable.toString()
                if (!oldTextString.equals(newTextString)) {
                    if (Character.codePointCount(
                            newTextString,
                            0,
                            newTextString.length
                        ) > maxCharactersAllowed
                    ) {
                        newTextString = oldTextString
                    }
                    editText.setText(newTextString)
                    editText.setSelection(newTextString.length)
                }
            }
        })
票数 -1
EN

Stack Overflow用户

发布于 2022-04-27 14:42:11

当有人输入一个表情符号时,你可以在该表情符号上调用.length,然后将你的最大字符计数增加那么多。(如果你想隐藏魔法的话,你必须记住你的原始字符计数,并在你的UI中使用它)。

也就是说,当有人键入一只“狗”,你会把你的最大计数从30增加到35。(1只被使用过,一只狗通常数到6只)

参考:

  1. https://twitter.com/riggaroo/status/1148278279713017858
  2. https://developer.android.com/reference/java/text/BreakIterator
  3. https://lemire.me/blog/2018/06/15/emojis-java-and-strings/
  4. https://developer.android.com/reference/kotlin/androidx/emoji/text/EmojiCompat
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72030560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档