Use Android text selection shifting API to handle keyboard backspace (flutter/engine#8956)

This commit is contained in:
Gary Qian
2019-05-15 10:44:23 +08:00
committed by GitHub
parent e21816be76
commit 04e41a7e6d

View File

@@ -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 @@ class InputConnectionAdaptor extends BaseInputConnection {
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 @@ class InputConnectionAdaptor extends BaseInputConnection {
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();