如何将键代码转换为char或String以在TextView上显示它

时间:2012-10-13 13:40:51

标签: java android keycode

  

可能重复:
  How can I convert a key code into a char or string?

嘿,我对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);
}

但是我想改变一个textview来说出按下了什么按钮。如何将keyCode传递给setText函数?我已将它全部链接起来了,我只需要将keyCode变量放入

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

1 个答案:

答案 0 :(得分:2)

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

int pressedKeyCode = event.getKeyCode();
Object pressedKeyCodeObject = new Integer(pressedKeyCode);
String pressedKeyText = KeyEvent.getKeyText((Integer)pressedKeyCodeObject);