Android: Set correct initial selection values in EditorInfo

Previously, when a TextInput field was pre-filled with text,
focusing the input would cause the keyboard to behave incorrectly.
Typing any character would duplicate the last word. This appeared
to be caused by the keyboard reapplying a composition segment
(e.g., the last word), without a proper pre-edit region
defined—resulting in the word being appended again.

This issue is resolved by correctly setting
EditorInfo.initialSelStart and EditorInfo.initialSelEnd when the
InputConnection is created. Properly initializing these values
ensures the IME (input method editor) can synchronize with the text
field correctly. It can be found in [0]documentation:

  "Also, take good care to fill in the EditorInfo object correctly and
   in its entirety, so that the connected IME can rely on its values.
   For example, EditorInfo.initialSelStart and EditorInfo.initialSelEnd
   members must be filled in with the correct cursor position for IMEs
   to work correctly with your application."

[0]https://developer.android.com/reference/android/view/View#onCreateInputConnection(android.view.inputmethod.EditorInfo)

Pick-to: 6.9 6.8 6.5
Fixes: QTBUG-135481
Change-Id: I5ae5a2fb22f285154ba3857033506d367bfd7642
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Reviewed-by: Rami Potinkara <rami.potinkara@qt.io>
This commit is contained in:
Bartlomiej Moskal 2025-04-17 07:28:13 +02:00
parent a867a16ef0
commit 6de8ce2af3

View File

@ -12,6 +12,8 @@ import android.graphics.Canvas;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.view.KeyEvent;
@ -107,6 +109,13 @@ class QtEditText extends View
outAttrs.imeOptions = m_imeOptions;
outAttrs.initialCapsMode = m_initialCapsMode;
m_inputConnection = new QtInputConnection(this,m_qtInputConnectionListener);
ExtractedText extracted = m_inputConnection.getExtractedText(new ExtractedTextRequest(), 0);
if (extracted != null) {
outAttrs.initialSelStart = extracted.selectionStart;
outAttrs.initialSelEnd = extracted.selectionEnd;
}
return m_inputConnection;
}