我有一个EditText,我在其上附加了一个TextWatcher。每次更改后,内容都会从ë
和ä
等特殊字符中删除。结果我把它放回到可编辑状态,以便在UI上显示。除三星Galaxy Note 2外,它适用于所有设备。
所以在Note上发生的事情是接受并显示第一个字符。输入第二个字符后,onTextChanged
和beforeTextChanged
都会s.length()
0
如果您注释掉Selection.setSelection(info, position);
它有效,但它将光标设置在开头。
TextWatcher
editTextWatcher = new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
// Prevent a stackoverflow
editText.removeTextChangedListener(this);
editText.setText(cleanDescription(s.toString()));
editText.addTextChangedListener(this);
editText.requestFocus();
int position = editText.length();
Editable info = editText.getText();
// Set the cursor at the end
Selection.setSelection(info, position);
}
};
清洁功能
private String cleanDescription(String info)
{
Log.d(TAG, "cleanDescription()");
if (info != null)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
{
info = Normalizer.normalize(info, Normalizer.Form.NFD);
}
info = info.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
info = info.replaceAll("[^a-zA-Z0-9\\s.,-]", "");
}
return info;
}
Logcat会发出警告
09-23 16:22:24.865: W/IInputConnectionWrapper(5086): setComposingText on inactive InputConnection
09-23 16:22:24.865: W/IInputConnectionWrapper(5086): getExtractedText on inactive InputConnection
09-23 16:22:24.870: W/IInputConnectionWrapper(5086): finishComposingText on inactive InputConnection