单击时更改Android操作栏菜单项的图标

时间:2013-08-17 18:37:52

标签: java android android-actionbar menuitem

我正在使用sherlock动作栏。 我在操作栏上有2个项目。选择项目(活动)后,我想更改图标的图像。

这是我在Java上的代码

    @Override
    public boolean onPrepareOptionsMenu (Menu menu){
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menutes, menu);
    todaySched=menu.findItem(R.id.todaySched);
    if(todaySched.isEnabled()){
        todaySched.setIcon(R.drawable.calendarselected);

    }
    return true;
}

但是当我这样做时,图标变为双倍,图标也不会改变。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

使用on onOptionsItemSelected方法

@Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.todaySched:

              // put your code here to change the icon
              return true;

          default:
              return super.onOptionsItemSelected(item);
      }
  }

您可能需要为ActionBar Sherlock库包含正确的命名空间,以确保它覆盖正确的菜单项。所以方法的开头看起来像这样:

@Override
  public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)
相关问题