我想检测时,在我的Android手机的物理菜单按钮已经pssed $ P $。我虽然code以下会工作,但事实并非如此。我要去哪里错了吗?
返回的错误是非法修改参数的onkeydown;只有最终被允许
公共布尔的onkeydown(INT键code,KeyEvent的事件){
如果(键code == KeyEvent.KEY code_MENU){
//做的东西
} 其他 {
返回super.onKeyDown(键code,事件);
}
}
解决方案
我会寻找一个的了的按键事件,而不是一个下的情况下,用的onkeyup
。
公共布尔的onkeyup(INT键code,KeyEvent的事件){
如果(键code == KeyEvent.KEY code_MENU){
// ........
返回true;
}
返回super.onKeyUp(键code,事件);
}
我们返回真
,因为我们正在处理事件;返回假
如果你希望系统来处理事件了。
您可以做到这一切在你的活动
实例太多,因为活动
是的KeyEvent 。
I am trying to detect when the physical Menu button on my Android phone has been pressed. I though the code below would work but it does not. Where am I going wrong please?
The error returned is 'Illegal modifier for parameter onKeyDown; only final is permitted'
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// Do Stuff
} else {
return super.onKeyDown(keyCode, event);
}
}
解决方案
I'd look for an up key event, rather than a down event, with onKeyUp
.
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// ........
return true;
}
return super.onKeyUp(keyCode, event);
}
We return true
because we're handling the event; return false
if you want the system to handle the event too.
You can do all of this in your Activity
instance too because Activity
is a known indirect subclass of KeyEvent
.