0

可能重复:
如何将键码转换为字符或字符串?

嘿,我对 Android 很陌生,不确定如何使用 getKeyCode。我有这个

public boolean onKeyDown(int keyCode, KeyEvent event)
{
    switch(keyCode)
    {
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        Toast.makeText(getApplicationContext(), "Pressed DPAD Right Button",
        Toast.LENGTH_LONG).show();
        return true;
    case KeyEvent.KEYCODE_DPAD_LEFT:
            Toast.makeText(this, "Pressed DPAD Left Button",
            Toast.LENGTH_LONG).show();
            return true;
    }
    return super.onKeyDown(keyCode,event);
}

但我想改变一个文本视图来说明按下了什么按钮。如何将 keyCode 传递给 setText 函数?我已经把它全部连接起来了,我只需要将 keyCode 变量放入

txt2.setText(Integer.toString(getKeyCode()));
4

1 回答 1

2

如果您想获得密钥的实际名称,可以执行以下操作:

int pressedKeyCode = event.getKeyCode();
Object pressedKeyCodeObject = new Integer(pressedKeyCode);
String pressedKeyText = KeyEvent.getKeyText((Integer)pressedKeyCodeObject);
于 2012-10-13T15:46:58.770 回答