Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f5fa03b

Browse files
authored
Use Android text selection shifting API to handle keyboard backspace (#8956)
1 parent 849fa42 commit f5fa03b

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
package io.flutter.plugin.editing;
66

77
import android.content.Context;
8+
import android.text.DynamicLayout;
89
import android.text.Editable;
10+
import android.text.Layout;
911
import android.text.Selection;
12+
import android.text.TextPaint;
1013
import android.view.KeyEvent;
1114
import android.view.View;
1215
import android.view.inputmethod.BaseInputConnection;
@@ -24,10 +27,12 @@ class InputConnectionAdaptor extends BaseInputConnection {
2427
private final Editable mEditable;
2528
private int mBatchCount;
2629
private InputMethodManager mImm;
30+
private final Layout mLayout;
2731

2832
private static final MethodChannel.Result logger =
2933
new ErrorLogResult("FlutterTextInput");
3034

35+
@SuppressWarnings("deprecation")
3136
public InputConnectionAdaptor(
3237
View view,
3338
int client,
@@ -40,6 +45,9 @@ public InputConnectionAdaptor(
4045
this.textInputChannel = textInputChannel;
4146
mEditable = editable;
4247
mBatchCount = 0;
48+
// We create a dummy Layout with max width so that the selection
49+
// shifting acts as if all text were in one line.
50+
mLayout = new DynamicLayout(mEditable, new TextPaint(), Integer.MAX_VALUE, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
4351
mImm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
4452
}
4553

@@ -144,7 +152,8 @@ public boolean sendKeyEvent(KeyEvent event) {
144152
return true;
145153
} else if (selStart > 0) {
146154
// Delete to the left of the cursor.
147-
int newSel = Math.max(selStart - 1, 0);
155+
Selection.extendLeft(mEditable, mLayout);
156+
int newSel = Selection.getSelectionEnd(mEditable);
148157
Selection.setSelection(mEditable, newSel);
149158
mEditable.delete(newSel, selStart);
150159
updateEditingState();

0 commit comments

Comments
 (0)