我在自己的应用中将TextChangedListener
添加到了一个EditText
中。在LG手机(LG G5,Android 6)或仿真器上测试应用程序时,TextChangedListener
正常运行。但是,三星手机(三星Galaxy S7,Android 8)在我的日志中引起警告:
10-18 16:30:06.369 932-932/my.package.com D/InputMethodManager: SSI - flag : 1 Pid : 932 view : my.package.com
10-18 16:30:06.384 932-932/my.package.com D/ViewRootImpl@d279474[ToMyCardActivity]: Relayout returned: old=[0,0][1440,2560] new=[0,0][1440,2560] result=0x1 surface={valid=true 503188893696} changed=false
10-18 16:30:06.521 932-932/my.package.com D/ViewRootImpl@d279474[ToMyCardActivity]: MSG_RESIZED: frame=Rect(0, 0 - 1440, 2560) ci=Rect(0, 84 - 0, 0) vi=Rect(0, 84 - 0, 840) or=1
10-18 16:30:08.759 932-932/my.package.com V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@32559fa nm : my.package.com ic=com.android.internal.widget.EditableInputConnection@9adabab
10-18 16:30:08.762 932-932/my.package.com I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
10-18 16:30:08.793 932-932/my.package.com V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@d925da1 nm : my.package.com ic=com.android.internal.widget.EditableInputConnection@b5f1bc6
10-18 16:30:08.794 932-932/my.package.com I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
10-18 16:30:08.809 932-932/my.package.com W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
10-18 16:30:09.149 932-932/my.package.com V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@ca9e523 nm : my.package.com ic=com.android.internal.widget.EditableInputConnection@4396a20
10-18 16:30:09.153 932-932/my.package.com I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
10-18 16:30:09.173 932-932/my.package.com W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
10-18 16:30:09.327 932-932/my.package.com V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@17fdd9e nm : my.package.com ic=com.android.internal.widget.EditableInputConnection@f2bb07f
10-18 16:30:09.329 932-932/my.package.com I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
10-18 16:30:09.342 932-932/my.package.com W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
10-18 16:30:09.833 932-932/my.package.com V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@351e095 nm : my.package.com ic=com.android.internal.widget.EditableInputConnection@48835aa
10-18 16:30:09.837 932-932/my.package.com I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
10-18 16:30:09.852 932-932/my.package.com W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
10-18 16:30:10.472 932-932/my.package.com V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@9965d38 nm : my.package.com ic=com.android.internal.widget.EditableInputConnection@6bcf111
10-18 16:30:10.476 932-932/my.package.com I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
10-18 16:30:10.504 932-932/my.package.com W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
10-18 16:30:11.072 932-932/my.package.com V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@f213077 nm : my.package.com ic=com.android.internal.widget.EditableInputConnection@f014e4
10-18 16:30:11.077 932-932/my.package.com I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
10-18 16:30:11.109 932-932/my.package.com W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
10-18 16:30:11.522 932-932/my.package.com V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@32dd002 nm : my.package.com ic=com.android.internal.widget.EditableInputConnection@36e5d13
10-18 16:30:11.528 932-932/my.package.com I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
10-18 16:30:11.560 932-932/my.package.com W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
10-18 16:30:11.792 932-932/my.package.com V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@c23b149 nm : my.package.com ic=com.android.internal.widget.EditableInputConnection@2acea4e
10-18 16:30:11.796 932-932/my.package.com I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
10-18 16:30:11.809 932-932/my.package.com W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
10-18 16:30:12.256 932-932/my.package.com V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@3171c7c nm : my.package.com ic=com.android.internal.widget.EditableInputConnection@f6d1905
10-18 16:30:12.261 932-932/my.package.com I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
10-18 16:30:12.288 932-932/my.package.com W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
10-18 16:30:12.693 932-932/my.package.com V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@7d25b8b nm : my.package.com ic=com.android.internal.widget.EditableInputConnection@4f78468
10-18 16:30:12.695 932-932/my.package.com I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
10-18 16:30:12.707 932-932/my.package.com W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
这使键入变得困难,并且每当我想通过按住后退按钮删除所有字符时,只会删除一个字符。
有什么问题,我该如何解决?
这是我的TextWatcher
:
public class AmountTextWatcher implements TextWatcher {
private Context context;
private EditText editText;
private TextView textView;
private String SUFFIX;
private String prevText;
public AmountTextWatcher(Context context, EditText editText, TextView textView) {
this.context = context;
this.editText = editText;
this.textView = textView;
this.SUFFIX = context.getResources().getString(R.string.usd);
prevText = "";
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
prevText = s.toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
editText.removeTextChangedListener(this);
int iniLength;
int endLength;
iniLength = editText.getText().toString().length();
String onlyDigits = s.toString().replaceAll("\\D+", "");
String onlyDigitsPrevText = prevText.replaceAll("\\D+", "");
boolean suffixChange = false;
if (iniLength != 1) {
suffixChange = !editText.getText().toString().substring(iniLength - SUFFIX.length() - 1).equals(" " + SUFFIX);//|| when user tries to type number between or after suffix characters
}
long amount = onlyDigits.isEmpty() ? 0 : Long.parseLong(onlyDigits);
if (textView != null) {
String paymentAmount = String.format("%s USD", displayFormat(amount));//actual actions is more different, this is just example
textView.setText(paymentAmount);
}
String formattedString;
int newCursorPos;
if (onlyDigits.isEmpty()) {//when user deletes all the text
formattedString = "";
newCursorPos = 0;
} else {
formattedString = displayFormat(amount) + " " + SUFFIX;
endLength = formattedString.length();
int sel = editText.getSelectionStart();
if (onlyDigits.equals(onlyDigitsPrevText) || iniLength == 1 || suffixChange) {//when user tries to delete blank spaces or suffix || when user types for the first time
newCursorPos = formattedString.length() - SUFFIX.length() - 1;
} else {
newCursorPos = sel + endLength - iniLength;
}
}
editText.setText(formattedString);
editText.setSelection(newCursorPos < 0 ? 0 : newCursorPos);
editText.addTextChangedListener(this);
}
private static String displayFormat(double src) {
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols();
otherSymbols.setDecimalSeparator('.');
otherSymbols.setGroupingSeparator(' ');
DecimalFormat decimalFormat = new DecimalFormat("#,###.##", otherSymbols);
return decimalFormat.format(src);
}
}