gpt4 book ai didi

android - 使用 InputConnection.commitText 将光标设置在插入文本的开头

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:51:33 33 4
gpt4 key购买 nike

InputConnection.commitText(CharSequence text, int newCursorPosition) 的文档表示 newCursorPosition 意味着:

int: The new cursor position around the text, in Java characters. If > 0, this is relative to the end of the text - 1; if <= 0, this is relative to the start of the text. So a value of 1 will always advance the cursor to the position after the full text being inserted. Note that this means you can't position the cursor within the text, because the editor can make modifications to the text you are providing so it is not possible to correctly specify locations there.

this example ,如果我输入两个字符,然后像这样将光标放在它们之间

enter image description here

然后再输入一个字符,我把newCursorPosition设为0或者1都没有关系。光标总是在插入的末尾。例如调用

inputConnection.commitText("aaa", 0);

inputConnection.commitText("aaa", 1);

两者都是这样显示光标的:

enter image description here

如果我用-1

inputConnection.commitText("aaa", -1);

我明白了

enter image description here

1-1 结果符合文档的预期。 为什么 0 不把光标放在插入的开头?我希望 0 应该是这样的

inputConnection.commitText("aaa", 0);

enter image description here

但事实并非如此。为什么不呢?

最佳答案

这看起来像是代码中的一个缺陷,但您是判断者。

看看replaceText()BaseInputConnection 中。我相信这是插入后放置光标的代码。 (replaceText()commitText() 调用)

在引用的代码中,a是选择的开始。 b是选择端。由于示例中没有选择并且光标位于索引 1 处,因此 a == b == 1。此外,在光标移动到新选择之前,不会插入新文本 (aaa)(替换选择 [a,b])。

Selection.setSelection(content, newCursorPosition) 设置光标位置,因此对于 0 和 1 在您的示例中产生相同的定位,我期望 newCursorPosition 的派生值> 两个输入相同。

将光标定位在位置 1 的两个 8 之间,让我们仔细思考以下代码:

if (newCursorPosition > 0) {
newCursorPosition += b - 1;
} else {
newCursorPosition += a;
}

对于您输入的 1,newCursorPosition > 0,因此 newCursorPosition = newCursorPosition + 1 - 1 或 1。

对于您输入的 0,newCursorPosition 不是 = 0,因此 newCursorPosition = newCursorPosition + a (0 + 1) 或 1。

由于两个输入产生相同的值,我希望 Selection.setSelection(content, newCursorPosition) 产生您看到的结果。

我没有完全按照代码到这个位置,但我相信这就是问题所在。 我已经按照 BaseInputConnection 中的执行路径为 newCursorPosition = 0newCursorPosition = 1 在具有 API 21 的 Pixel 模拟器上以及上面概述的内容确实成立。

关于android - 使用 InputConnection.commitText 将光标设置在插入文本的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45178646/

33 4 0
文章推荐: javascript - Backbone.js: Uncaught TypeError: Object # 没有方法 'get'