我正在使用自定义键盘并使用 deleteSurroundingText
删除字符。我对此只有两个问题。 deleteSurroundingText
在删除表情符号时效果不佳。我需要按 del
按钮两次才能删除单个表情符号。第二个 del
键不适用于 select all
选项。
case Keyboard.KEYCODE_DELETE:
getCurrentInputConnection().deleteSurroundingText(1,0);
break;
当我按下尝试删除表情符号时,表情符号会发生以下情况:
?
它变成了一个问号。
此外,当我尝试通过执行 select all
删除文本时,什么也没有发生。
任何帮助将不胜感激
最佳答案
Java 使用 16 位字符(参见文档中的 note)。所以一个字符可以存储从U+0000
到U+FFFF
的codepoint。
现代 unicode 定义代码点范围从 U+0000
到 U+10FFFF
。
大多数表情符号的代码点都在 U+FFFF
之外。为了表示这样的代码点,使用了所谓的“surrogate pairs”。
换句话说,每个表情符号(以及 U+FFFF
边界之外的所有其他代码点)都由字符串中的 两个 后续字符表示。
当您调用 deleteSurroundingText(1,0);
时,您会损坏代理项对。尚未删除的代理对部分呈现为 ?
标记。
deleteSurroundingText() 的文档特别强调这种情况:
IME authors: please be careful not to delete only half of a surrogate pair. Also take care not to delete more characters than are in the editor, as that may have ill effects on the application. Calling this method will cause the editor to call onUpdateSelection(int, int, int, int, int, int) on your service after the batch input is over.
请下次尝试使用前仔细阅读方法文档。
要确定字符是否是代理对的一部分,请使用 Chracter::isSurrogate()方法。
关于java - 为什么 deleteSurroundingText 不适用于表情符号并全选?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47853404/