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

发布于 2022-04-28 08:41:43
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)
}
}
})发布于 2022-04-27 14:42:11
当有人输入一个表情符号时,你可以在该表情符号上调用.length,然后将你的最大字符计数增加那么多。(如果你想隐藏魔法的话,你必须记住你的原始字符计数,并在你的UI中使用它)。
也就是说,当有人键入一只“狗”,你会把你的最大计数从30增加到35。(1只被使用过,一只狗通常数到6只)
参考:
https://stackoverflow.com/questions/72030560
复制相似问题