经过大量研究,这就是我想出的。我使用了一个KeyListener
接受关键事件的。Editable
然后它基于关键事件(考虑班次等)附加到一个。随着关键事件的到来,下面的代码将简单地回显不断增长的字符串。
KeyListener tkl = new TextKeyListener(Capitalize.NONE, false);
Editable et = Editable.Factory.getInstance().newEditable("");
View.OnKeyListener key = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
tkl.onKeyDown(null, et, keyCode, event);
else if (event.getAction() == KeyEvent.ACTION_UP)
tkl.onKeyUp(null, et, keyCode, event);
else
tkl.onKeyOther(null, et, event); //NOTE: My devices never used KeyEvent.ACTION_MULTIPLE so I don't know if it should get fired here or with the key down event.
Log.i(("Test: " + et.toString());
}
};
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (key.onKey(null, event.getKeyCode(), event))
return true;
else
return super.dispatchKeyEvent(event);
}