diff --git a/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java b/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java index c613e82ff3a01..02010b13c467b 100644 --- a/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java +++ b/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java @@ -5,8 +5,11 @@ package io.flutter.plugin.editing; import android.content.Context; +import android.text.DynamicLayout; import android.text.Editable; +import android.text.Layout; import android.text.Selection; +import android.text.TextPaint; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.BaseInputConnection; @@ -24,10 +27,12 @@ class InputConnectionAdaptor extends BaseInputConnection { private final Editable mEditable; private int mBatchCount; private InputMethodManager mImm; + private final Layout mLayout; private static final MethodChannel.Result logger = new ErrorLogResult("FlutterTextInput"); + @SuppressWarnings("deprecation") public InputConnectionAdaptor( View view, int client, @@ -40,6 +45,9 @@ public InputConnectionAdaptor( this.textInputChannel = textInputChannel; mEditable = editable; mBatchCount = 0; + // We create a dummy Layout with max width so that the selection + // shifting acts as if all text were in one line. + mLayout = new DynamicLayout(mEditable, new TextPaint(), Integer.MAX_VALUE, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); mImm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); } @@ -144,7 +152,8 @@ public boolean sendKeyEvent(KeyEvent event) { return true; } else if (selStart > 0) { // Delete to the left of the cursor. - int newSel = Math.max(selStart - 1, 0); + Selection.extendLeft(mEditable, mLayout); + int newSel = Selection.getSelectionEnd(mEditable); Selection.setSelection(mEditable, newSel); mEditable.delete(newSel, selStart); updateEditingState();