Android中添加Options Menu,按MENU键无反应
自已开发的一个日历项目,其中一个显示日程列表的Activity只有一个ListView,数据来源于数据库。
在这个xxxActivity.java里面添加了:
/** Create menu. */
public booleanonCreateOptionsMenu(Menu menu) {
MenuInflater mMenuInflater = getMenuInflater();
mMenuInflater.inflate(R.menu.menu_for_agenda_list, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
/** Judge the item selected. */
public booleanonOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected()");
switch (item.getItemId()) {
case R.id.newAgendaItem:
break;
case R.id.clearAgendaList:
break;
case R.id.returnBack:
break;
default:
Log.d(TAG, "other");
break;
}
return super.onOptionsItemSelected(item);
}
但是,运行后,点MENU键,不弹出optionmenu菜单,毫无反应!后来经过仔细排查,发现是因为自已重写的onKeyDown返回值置为true而造成的,改为false,使按键事件继续沿响应链处理,问题即解决了!
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.finish();
}
return false;// 一定要为false,若为true,则MENU按键事件终止了在响应链中的传递!
}
其中menu_for_agenda_list.xml文件如下
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/newAgendaItem" android:title="新建" android:icon="@drawable/agenda_list_menu_new"/>
<item android:id="@+id/clearAgendaList" android:title="清空" android:icon="@drawable/agenda_list_menu_clear"/>
<item android:id="@+id/returnBack" android:title="返回" android:icon="@drawable/agenda_list_menu_return"/>
</menu>
!!!文件内容大了,排查了好久才找到原因!这里一定要记录一下!