在Android中通过自定义键盘在EditText上设置撰写文本 [英] Set composing text on an EditText from a custom keyboard in Android

查看:600
本文介绍了在Android中通过自定义键盘在EditText上设置撰写文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个自定义应用内键盘,该键盘的工作原理与此示例相同.但是,在键盘上,我使用弹出窗口来显示多余的字母形式.在传统蒙古语中,字母的格式不同,取决于它们是否位于开头,单词的中间或结尾.通常,这些可以从上下文中确定,但是有时用户需要从弹出键候选中显式选择一种替代形式.

I'm making a custom in-app keyboard that works on the same principle of this example. However, in my keyboard I'm using popup windows to display extra letter forms. In traditional Mongolian letters have different forms depending on if they are located at the beginning, middle, or end of a word. Usually these can be determined from the context, but sometimes a user needs to choose an alternate form explicitly from the popup key candidates.

让我们说用户开始输入单词(其中-代表字母):

Lets say that a user starts typing a word (where - represent letters):

--- 

然后他们从弹出窗口中选择了a(我仅使用a来代表选择特殊蒙古字形形式的概念).如果他们继续输入,则此字母的Unicode将呈现如下:

Then they chose a from the popup (I'm only using a to represent the concept of choosing a special Mongolian glyph form). The Unicode for this letter will be rendered as follows if they continue typing:

---a--

但是,Unicode呈现为

However, the Unicode is rendered as

---A

字尾的

. (aA在Unicode中具有相同的代码.)因此,用户感到困惑,为什么他们从弹出键中选择a,但在编辑器中将其呈现为A.不过,如果他们只是继续键入文字,那会很好,因为它会在单词中间呈现为a.

at the end of words. (a and A have the same code in Unicode.) So the user is confused why they chose a from the popup key but it gets rendered as A in the editor. If they would just keep typing, though, it will be fine since it gets rendered as a in middle of words.

我要执行的操作在---aa上设置了某种临时跨度,以便在键入下一个字母之前不会呈现为---A.但是,如果他们添加空格或将光标移动到其他位置,则它将恢复为默认的---A形式以表示最后一个字母. (也就是说,临时跨度将被取消.)

What I want to do it set some sort of temporary span on the a of ---a so that it doesn't get rendered as ---A before they type the next letter. But if they add a space or move the cursor to a different location, then it will revert back to the default ---A form for final letters. (That is, the temporary span will be cancelled.)

真实示例

如果抽象aA太混乱了,这是一个真实的例子.用户想在此词中输入蒙古语UE形式(Unicode \u1826\u180c)

If the abstract a and A are too confusing, here is a real example. The user wants to type a Mongolian UE form (Unicode \u1826\u180c) in this word

但是,由于\u1826\u180c在单词结尾处呈现为

But since \u1826\u180c gets rendered like this at the end of words

用户一直困惑直到他们继续输入.我想让跨度看起来像这样

the user is confused until they continue typing. I want the span to make it look like this

可以用\u1826\u180c\u200d临时呈现.

文档

如果您的IME进行文字预测或需要多个步骤来撰写 字形或字词,您可以在文本字段中显示进度,直到 用户提交单词,然后您可以替换部分单词 与完整的文字组成.

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);

我将在本节中描述为什么它不起作用,但是在这里设置问题的过程中,我发现它确实起作用.因此,我将在下面添加我的答案作为示例,以说明其他人在做类似的事情.

I was going to describe why it wasn't working in this section, but in the process of setting up my question here, I discovered that it actually works. So I will add my answer below as an example for other people are who doing something similar.

推荐答案

以下代码设置了一个临时组成跨度,当有问题的字符串由弹出窗口返回时

The following code sets a temporary composing span when the string in question is returned by the popup window

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

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

where selectedItem is the string chosen by the user from the key popup window candidates.

请注意,a有一个下划线,指示它是一个合成跨度. (这是一个人为问题的示例,如果立即提交文本,则a将被呈现为A.)

Note that the a has an underline indicating that it is a composing span. (This is a contrived example from the question where a would be rendered as A if the text were committed immediately.)

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

This also works for the real example in the question

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

当确认用户想要保持构图范围(即他们继续在单词中输入更多字母)时,可以使用

When it is confirmed that the user wants to keep the composing span (ie, they keep typing more letters in the word), it can be committed with

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

放弃合成跨度

如果用户单击其他位置,则不会取消构图范围.但这是一个不同的问题.

Abandoning the composing span

If the user clicks somewhere else, the composing span is not cancelled. But this is a different question.

您的键盘可以覆盖onUpdateSelection,以监听那里的光标更改.然后致电

Your keyboard can override onUpdateSelection to listen for cursor changes there. Then call

inputConnection.finishComposingText();

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

to keep whatever text was in the composing region. Or

ic.commitText("", 1);

摆脱它.有关更多信息,请参见此答案.

to get rid of it. See this answer for more.

这篇关于在Android中通过自定义键盘在EditText上设置撰写文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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