0

我正在使用 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 方法。

这有什么问题?提前致谢。

4

1 回答 1

0

I found the cause. The above activity extends CommonActivity. CommonActivity already overided onMenuItemSelected method. So I override onMenuItemSelected method like below:

  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);
        }
于 2012-07-16T01:42:52.873 回答