gpt4 book ai didi

java - 如何将关键代码转换为字符或字符串以在 TextView 上显示

转载 作者:行者123 更新时间:2023-12-02 07:31:28 25 4
gpt4 key购买 nike

Possible Duplicate:
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()));

最佳答案

如果您想获取 key 的实际名称,您可以执行以下操作:

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

关于java - 如何将关键代码转换为字符或字符串以在 TextView 上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12873657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com