在我的应用程序中,我有一个带有EditText的片段,它实现了一个textWatcher来设置掩码。。。它在Android 27之前运行良好(奥利奥(,但在奥利奥上,我注意到有时它不能正常工作。。。就像它被称为两次
即使我只是再次删除TextChangedListener、settext和addTextChangedListener,有时它也会重复一个字符。如果我擦除一个字符,它也会发生。
etCPF.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
etCPF.removeTextChangedListener(this);
etCPF.setText(charSequence);
etCPF.setSelection(etCPF.getText().length());
etCPF.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable editable) { }
});
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textNoSuggestions" />
<Button
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Search" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvResult"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
示例:如果我在键盘上按as,在编辑文本上只显示为或ass
在日志屏幕中,我得到了这个:
08-07 15:22:23.053 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.093 1850-1850/com.google.android.inputmethod.latin W/GInputConnectionWrapper: Fixing selection indices to 1, 1
08-07 15:22:23.119 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1219] vs decoder [1220]
08-07 15:22:23.897 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.898 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1226] vs decoder [1227]
08-07 15:22:23.907 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
getTextAfterCursor on inactive InputConnection
08-07 15:22:23.910 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
08-07 15:22:23.912 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.913 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
08-07 15:22:23.915 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
08-07 15:22:23.916 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
08-07 15:22:23.917 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
08-07 15:22:23.918 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
getSelectedText on inactive InputConnection
08-07 15:22:23.919 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1231] vs decoder [1232]
08-07 15:22:23.920 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.922 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
08-07 15:22:23.923 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
getTextAfterCursor on inactive InputConnection
getSelectedText on inactive InputConnection
endBatchEdit on inactive InputConnection
08-07 15:22:23.926 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
getTextAfterCursor on inactive InputConnection
getSelectedText on inactive InputConnection
08-07 15:22:23.927 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.930 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1235] vs decoder [1236]
08-07 15:22:23.949 1850-1850/com.google.android.inputmethod.latin W/GInputConnectionWrapper: Fixing selection indices to 3, 3
以前有人有这个问题吗?以前有人有这个问题吗?
这是我在评论中所说的一个小例子。
示例
if (etCPF == nul) {
etCPF = (EditText) findViewById(R.id.editText)
etCPF.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
etCPF.removeTextChangedListener(this);
etCPF.setText(charSequence);
etCPF.setSelection(etCPF.getText().length());
etCPF.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable editable) { }
});
}
为什么会这样,是因为我认为我们在生命周期方法中比在Activity
时通过示例旋转重复,然后添加许多时间TextWatcher
。所以这可以解释为什么他们有很多TextWatcher