我正在为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)
}
...
出于某种原因,commitCorrection(( 似乎不起作用。因此,对于我们的软键盘,我们实施了一个解决方法解决方案:
val correctionInfo: CorrectionInfo = ... // an instance of CorrectionInfo
val cursorStart = ... // Current position of the cursor in the input field
val original = correctionInfo.oldText as String
val replace = correctionInfo.newText as String
val offset = correctionInfo.offset
val editingEnd = offset + original.length
// Setting the value to 1 leaves the cursor at the end of the text, the 'offset' to previous cursor position is added later.
val newCursorPosition = 1
inputConnectionLocal.beginBatchEdit()
// Adds the 'offset' to the previous cursor position. This is important for the cases when the user has managed to type something while the current autocorrection is still going on.
val cursorOffset = cursorStart - editingEnd
inputConnectionLocal.setComposingRegion(offset, editingEnd)
inputConnectionLocal.setComposingText(replace, newCursorPosition + cursorOffset)
inputConnectionLocal.finishComposingText()
// Fix for Teams and similar apps that seem to ignore the offset set in setComposingText
val additionalChars = replace.length - original.length
inputConnectionLocal.setSelection(cursorStart + additionalChars, cursorStart + additionalChars)
inputConnectionLocal.endBatchEdit()
inputConnectionLocal.requestCursorUpdates(InputConnection.CURSOR_UPDATE_IMMEDIATE)