我在 EditText 上设置了一个 TextWatcher,它在用户键入一个数字后跟一个空格后更改输入类型。
如果用户输入两个数字,输入类型会切换并接受下一个字符,但如果用户只输入一个数字并按空格键,输入类型仍然会改变,但不会接受用户尝试输入的第一个字符。
我已经在 Froyo 和 1.6 上对此进行了测试,它只发生在 Froyo 上,1.6 可以正常工作。
这是代码:
TextWatcher watcher = new TextWatcher() {
@Override
public void afterTextChanged (Editable s) {
}
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
// Parsed text holder is a class that just parses the EditText and pulls out various parts.
ParsedTextHolder th = parseTextHolder(s);
String newText = "";
Boolean setTextKeyListener = false;
String tGetTextString = mQuery.getText().toString();
if (!th.pFullMatch.equals("")) {
if (th.pFullMatch.length() == 2) {
mQuery.setKeyListener(new
TextKeyListener(TextKeyListener.Capitalize.SENTENCES, true));
newText = tGetTextString + " for ";
setTextKeyListener = true;
}
}
if (setTextKeyListener) {
Log.i("setTextKeyListener", "true");
if (mQuery.getKeyListener().getClass() != TextKeyListener.class) {
mQuery.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.SENTENCES, true));
} else {
Log.d("setTextKeyListener", "skipped. already was text.");
}
if (!newText.equals("")) {
int position = newText.length();
String ttext = newText;
newText = "";
mQuery.setText(ttext, TextView.BufferType.EDITABLE);
mQuery.setText(ttext);
Editable text = mQuery.getEditableText();
Log.w("setting selectiont to text: ", text.toString());
Log.w("setting selectiont to position: ", Integer.toString(position));
Selection.setSelection(text, position);
mQuery.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.SENTENCES, true));
}
}
}
};
此外,如果您想查看错误是什么样的,这里有一个 APK:http: //endlesswhileloop.com/files/KeyboardBug.apk