gpt4 book ai didi

android - onKey onKeyDown 不工作

转载 作者:行者123 更新时间:2023-11-29 19:33:47 30 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用程序中注册方向键/电视遥控方向键点击。我目前正在使用 android 模拟器进行测试,我正在尝试在额外设置菜单下点击方向键输入。但我不确定为什么这不起作用 - 任何帮助将不胜感激

public class FullscreenActivity extends AppCompatActivity {

private View mContentView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);

mContentView = findViewById(R.id.fullscreen_content);

mContentView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d("debug", "we are here");
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
Log.d("keycode", "center pressed");
return true;
}
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.d("OnKey", "key pressed!");
return true;
}
return false;
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
}

运行这段代码在 logcat 上没有任何输出

最佳答案

您可以覆盖 Activity 的 onKeyDown() 方法来检测按键。

试试这个

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

if (event.getAction() == KeyEvent.ACTION_DOWN) {

switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_UP:
showToast("UP pressed");
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
showToast("DOWN pressed");
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
showToast("RIGHT pressed");
return true;
case KeyEvent.KEYCODE_DPAD_LEFT:
showToast("LEFT pressed");
return true;
case KeyEvent.KEYCODE_DPAD_CENTER:
showToast("CENTER pressed");
return true;
}
}
return super.onKeyDown(keyCode, event);
}

void showToast(String msg)
{
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
}

关于android - onKey onKeyDown 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39517637/

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