gpt4 book ai didi

java - 当我按下一个键时 onKeyUp 没有启动

转载 作者:行者123 更新时间:2023-12-02 10:42:27 27 4
gpt4 key购买 nike

每次我按下一个键来启动 toast 时,我都会尝试,但似乎没有启动它,知道吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.txtKey);
editText = findViewById(R.id.etxtKeyboard);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_P:
Toast.makeText(this, "P pressed", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onKeyUp(keyCode, event);
}
}

最佳答案

正如 Android 文档中关于 Activity.onKeyUp() 方法的描述:

Called when a key was released and not handled by any of the views inside of the activity. So, for example, key presses while the cursor is inside a TextView will not trigger the event (unless it is a navigation to another object) because TextView handles its own key presses.

因此,当焦点位于 findViewById(R.id.etxtKeyboard); 编辑 TextView 时,您似乎正在 try catch onKeyUp() 事件。

您可以尝试在Activity.onCreate()中添加这样的代码:

editText.setOnKeyListener(
new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_P:
Toast.makeText(MyActivity.this, "P pressed", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
}
);

关于java - 当我按下一个键时 onKeyUp 没有启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52838963/

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