安卓:我怎样才能设置一个监听器的菜单按钮?
问题描述:
我希望做一个自定义操作时,手机上的菜单pressing
按钮。
I want to do a custom action when pressing on the Menu
button on the phone.
是否有可能在按钮上设置onClickListener(或类似),如果是这样,怎么样?
Is it possible to set an onClickListener (or similar) on the button and if so, how?
onCreateOptionsMenu
只调用了第一次按钮pressed - 我已经试过这种
onCreateOptionsMenu
is only called the first time the button is pressed - I've already tried this.
答
通常你不应该重写菜单
行为,用户所期望出现的菜单,但是你可以使用的东西沿着这些路线:
Usually you shouldn't override MENU
behavior as users expect menu to appear, however you can use something along these lines:
/* (non-Javadoc)
* @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
Log.d(TAG, "MENU pressed");
return true;
}
return super.onKeyDown(keyCode, event);
}