Change Android Action Bar Menu Item's Icon on Click

你离开我真会死。 提交于 2019-12-11 02:36:56

问题


I'm using sherlock action bar. I have 2 items on the action bar. When the item is chosen (active), I want to change the icon's image.

This is my code on 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;
}

but when I do this the icon become double, and the icon won't change neither. Can someone help?


回答1:


Use the on onOptionsItemSelected method

@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);
      }
  }

you may need to include the correct namespace for the ActionBar Sherlock library to ensure it Overrides the correct menu item. So the start of the method will look like this:

@Override
  public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)


来源:https://stackoverflow.com/questions/18292126/change-android-action-bar-menu-items-icon-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!