我正在开发一款需要能够使用外置USB或蓝牙键盘楔的应用。然后我捕获KeyEvents并根据需要缓存它们(开始/结束标记)。
所以此时我需要将KeyEvents列表转换为字符串。
所以我需要将这些有序的KeyEvent转换为正确的字符串。
List<KeyEvent> events = new ArrayList<KeyEvent>();
String eventString = eventsToString(events); //??????
有什么想法吗?
答案 0 :(得分:2)
经过大量研究,这就是我想出的。我使用了KeyListener
接受关键事件。然后根据关键事件(考虑轮班等)将其附加到Editable
。当关键事件进入时,下面的代码将简单地回应不断增长的String。
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);
}
答案 1 :(得分:1)
如何为每个事件使用getDisplayLabel()?类似的东西:
List<KeyEvent> events = new ArrayList<KeyEvent>();
StringBuilder strEvents = new StringBuilder();
for (KeyEvent k : events) {
strEvents.append(k.getDisplayLabel())
strEvents.append(",");
}
String eventString = etrEvents.toString();
答案 2 :(得分:0)
仅使用getUnicodeChar()在vaimvolk循环上构建? http://developer.android.com/reference/android/view/KeyEvent.html#getUnicodeChar()