From eb3291f32d011e1aa5dc24259a90dc640da53d16 Mon Sep 17 00:00:00 2001 From: Bartlomiej Moskal Date: Thu, 17 Apr 2025 07:28:13 +0200 Subject: [PATCH] Android: Set correct initial selection values in EditorInfo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.8 6.5 Fixes: QTBUG-135481 Change-Id: I5ae5a2fb22f285154ba3857033506d367bfd7642 Reviewed-by: Assam Boudjelthia Reviewed-by: Rami Potinkara (cherry picked from commit 6de8ce2af383c8d164c65ffca7dc845c52e4f2d2) Reviewed-by: Qt Cherry-pick Bot --- .../jar/src/org/qtproject/qt/android/QtEditText.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/android/jar/src/org/qtproject/qt/android/QtEditText.java b/src/android/jar/src/org/qtproject/qt/android/QtEditText.java index 4a0fd0a705f..79774961894 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtEditText.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtEditText.java @@ -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; }