gpt4 book ai didi

android - 通过 Android 中的自定义键盘在 EditText 上设置撰写文本

转载 作者:行者123 更新时间:2023-11-29 02:39:32 27 4
gpt4 key购买 nike

解释我想做什么

我正在制作一个自定义应用内键盘,其工作原理与 this example 相同.但是,在我的键盘中,我使用弹出窗口来显示额外的字母形式。在 traditional Mongolian字母有不同的形式,这取决于它们位于单词的开头、中间还是结尾。通常这些可以从上下文中确定,但有时用户需要从弹出键候选者中明确选择替代形式。

假设用户开始输入一个单词(其中 - 代表字母):

--- 

然后他们从弹出窗口中选择了a(我只是使用a来表示选择特殊蒙古字形的概念)。如果他们继续输入,这封信的 Unicode 将呈现如下:

---a--

但是,Unicode 呈现为

---A

在词尾。 (aA 在 Unicode 中具有相同的代码。)所以用户很困惑为什么他们从弹出键中选择了 a 但是它被渲染了作为编辑器中的 A。不过,如果他们继续打字,那就没问题了,因为它会在单词中间呈现为 a

我想做的是在 ---aa 上设置某种临时跨度,这样它就不会呈现为 - --A 在他们键入下一个字母之前。但是如果他们添加一个空格或将光标移动到不同的位置,那么它将恢复为默认的 ---A 形式的最后一个字母。 (即取消临时跨度。)

实例

如果抽象的aA太容易混淆,这里有一个真实的例子。用户想在这个词中输入蒙古文UE形式(Unicode \u1826\u180c)

enter image description here

但是因为 \u1826\u180c 在单词的末尾被渲染成这样

enter image description here

用户在继续输入之前会感到困惑。我想让 span 看起来像这样

enter image description here

可以用\u1826\u180c\u200d临时渲染。

文档

documentation

If your IME does text prediction or requires multiple steps to compose a glyph or word, you can show the progress in the text field until the user commits the word, and then you can replace the partial composition with the completed text.

它给出了这个例子和图片:

InputConnection ic = getCurrentInputConnection();
ic.setComposingText("Composi", 1);
ic.setComposingText("Composin", 1);
ic.commitText("Composing ", 1);

enter image description here

问题

我打算在本节中描述它为什么不起作用,但在此处设置问题的过程中,我发现它确实有效。所以我将在下面添加我的答案作为其他人做类似事情的例子。

最佳答案

以下代码设置了一个临时的 composing span当弹出窗口返回有问题的字符串时

if (selectedItem.equals("a")) {
inputConnection.setComposingText("a", 1);
}

其中 selectedItem 是用户从关键弹出窗口候选中选择的字符串。

enter image description here

请注意,a 有一个下划线,表明它是一个组合跨度。 (这是一个人为的例子,如果立即提交文本,a 将呈现为 A。)

这也适用于问题中的真实示例

if (selectedItem.equals("\u1826\u180c")) {
inputConnection.setComposingText("\u1826\u180c\u200d", 1);
}

enter image description here

提交组合跨度

当确认用户想要保持书写跨度(即,他们继续在单词中输入更多字母)时,可以提交

inputConnection.commitText("\u1826\u180c", 1);

放弃组合跨度

如果用户点击其他地方,合成跨度不会被取消。但这是一个不同的问题。

您的键盘可以覆盖 onUpdateSelection 以监听那里的光标更改。然后调用

inputConnection.finishComposingText();

保留组成区域中的任何文本。或者

ic.commitText("", 1);

摆脱它。参见 this answer了解更多。

关于android - 通过 Android 中的自定义键盘在 EditText 上设置撰写文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45204305/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com