gpt4 book ai didi

android - InputConnection.commitCorrection() 似乎无法正常工作

转载 作者:太空狗 更新时间:2023-10-29 13:53:07 29 4
gpt4 key购买 nike

我正在为 Android 开发软键盘。我想使用 InputConnection.commitCorrecrion() 更正一些文本,如果按下对应于 Keyboard.KEYCODE_DONE 的键。但是,文本没有改变,它只是闪烁一次。我该如何解决这个问题?

public class SimpleIME extends InputMethodService
implements KeyboardView.OnKeyboardActionListener {
....

@Override
public void onKey(int primaryCode, int[] keyCodes) {
InputConnection ic = getCurrentInputConnection();
switch(primaryCode){
....

case Keyboard.KEYCODE_DONE:
ic.commitCorrection(new CorrectionInfo(oldTextPosition, oldText, newText));
break;
....
}
}

最佳答案

我有一个类似的问题,但没有闪光灯,根本没有对文本进行任何更改。我认为 EditText 输入只是没有响应此 commitCorrection 调用或出于某种原因不接受它,所以我改用 deleteSurroundingText 和 commitText(我正在替换光标所在的任何单词,如果有的话):

 // limit=0 allows trailing empty strings to represent end-of-string split
fun getWordBeforeCursor() : String {
val arr = wordbreak.split(ic.getTextBeforeCursor(255, 0), 0)
Log.d(TAG, "words before: " + arr.joinToString(","))
return if (arr.isEmpty()) "" else arr.last()
}

fun getWordAfterCursor() : String {
val arr = wordbreak.split(ic.getTextAfterCursor(255, 0), 0)
Log.d(TAG, "words after: " + arr.joinToString(","))
return if (arr.isEmpty()) "" else arr.first()
}


fun getCursorPosition() : Int {
val extracted: ExtractedText = ic.getExtractedText(ExtractedTextRequest(), 0);
return extracted.startOffset + extracted.selectionStart;
}

try {
...

val backward = getWordBeforeCursor()
val forward = getWordAfterCursor()

Log.d(TAG, "cursor position: " + getCursorPosition())
Log.d(TAG, "found adjacent text: [" + backward + "_" + forward + "]")

// if in the middle of a word, delete it
if (forward.isNotEmpty() && backward.isNotEmpty()) {
//instead of this:
//ic.commitCorrection(CorrectionInfo(getCursorPosition()-backward.length, backward + forward, icon.text))

//do this:
ic.deleteSurroundingText(backward.length, forward.length)
ic.commitText(newText, 1)
}
...

关于android - InputConnection.commitCorrection() 似乎无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43298901/

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