我正在使用Android ICS 4.0.3,我下载了Ancal项目并进行了研究。我在一个活动中添加了一些选项菜单,但它无法调用opOptionsItemSelected方法。这是我的代码:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
String displayText = dateFormatFull.format(new Date());
switch(iCurrentAgendaViewType) {
case AgendaView.viewMode.DAY:
displayText = dateFormatFull.format(CurrentAgendaView.GetViewStartDate().getTime()).toString();
break;
case AgendaView.viewMode.WEEK:
displayText = dateFormatFull.format(CurrentAgendaView.GetViewStartDate().getTime()).toString();
break;
case AgendaView.viewMode.MONTH:
displayText = dateFormatMonth.format(CurrentAgendaView.GetCurrentSelectedMonthAsCalendar().getTime()).toString();
break;
}
if (iCurrentAgendaViewType == AgendaView.viewMode.TODAY) {
menu.add(Menu.NONE, android.R.id.button2, 1, displayText).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
} else {
menu.add(Menu.NONE, R.drawable.ic_arrow_left, 0, "").setIcon(R.drawable.ic_arrow_left).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, android.R.id.button2, 1, displayText).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, R.drawable.ic_arrow_right, 2, "").setIcon(R.drawable.ic_arrow_right).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (DEBUG) {
Log.d(TAG, "============ onOptionsItemSelected ===========");
}
switch (item.getItemId())
{
case R.drawable.ic_arrow_left:
CurrentAgendaView.SetPrevViewItem();
RefreshAgendaAfterViewItemChange();
return true;
case R.drawable.ic_arrow_right:
CurrentAgendaView.SetNextViewItem();
RefreshAgendaAfterViewItemChange();
return true;
case miNewAppt:
openActAppointment(-1, -1, -1);
return true;
case miNewTask:
openActTask(-1);
return true;
case miNewNote:
openActNote(-1);
return true;
case miShowAllTasks:
{
item.setChecked(!item.isChecked());
prefs.bShowAllTasks = item.isChecked();
prefs.Save();
refreshData();
menuItemUpdateIcons(item);
return true;
}
case miOptions:
openActOptions();
return true;
case mTimeZone:
showTimeZone();
return true;
case miAbout:
openActViewAbout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我在运行时添加了操作栏菜单,就像图像一样,但是当我调试上面的代码时,没有任何调用onOptionsItemSelected方法。
这有什么问题? 提前谢谢。
答案 0 :(得分:0)
我找到了原因。上述活动扩展了CommonActivity。 CommonActivity已经覆盖了onMenuItemSelected方法。所以我重写了onMenuItemSelected方法,如下所示:
public abstract class CommonActivity extends Activity {
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
if (item.getItemId() == android.R.id.home){
finish();
}
return true;
}
}
public class AnCal extends CommonActivity implements OnNavigationListener{
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
String displayText = getCurrentViewDateText();
if (iCurrentAgendaViewType == AgendaView.viewMode.TODAY) {
menu.add(Menu.NONE, android.R.id.button2, 1, displayText).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
} else {
menu.add(Menu.NONE, android.R.id.button1, 0, "Prev").setIcon(R.drawable.ic_arrow_left).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, android.R.id.button2, 1, displayText).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, android.R.id.button3, 2, "Next").setIcon(R.drawable.ic_arrow_right).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
return super.onCreateOptionsMenu(menu);
}