onMenuItemClick不会被调用

onMenuItemClick不会被调用

本文介绍了onMenuItemClick不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NavigationDrawer与ActionBarSherlock。当我点击菜单项 onMenuItemClick()方法不被调用。

在code:

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){    如果(item.getItemId()== android.R.id.home){        如果(mDrawerLayout.isDrawerOpen(mDrawerList)){            mDrawerLayout.closeDrawer(mDrawerList);
        }其他{            mDrawerLayout.openDrawer(mDrawerList);
        }
    }
    返回super.onOptionsItemSelected(项目);
}@覆盖
prepareOptionsMenu(菜单菜单)上公共布尔{    布尔drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    返回super.on prepareOptionsMenu(菜单);
}@覆盖
保护无效onPostCreate(捆绑savedInstanceState){    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){    super.onConfigurationChanged(NEWCONFIG);
    mDrawerToggle.onConfigurationChanged(NEWCONFIG);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){    的setTitle(AndroidUtils.getTitle());
    子菜单subMenu1 = menu.addSubMenu(NULL);
    subMenu1.add(getString(R.string.save_loc)).setIcon(android.R.drawable.ic_menu_save).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener(){        @覆盖
        公共布尔onMenuItemClick(菜单项项){            ...
        }
    });
    subMenu1.add(getString(R.string.save_em)).setIcon(android.R.drawable.ic_dialog_email).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener(){        @覆盖
        公共布尔onMenuItemClick(菜单项项){            ...
        }
    });
    菜单项subMenu1Item = subMenu1.getItem();
    subMenu1Item.setIcon(android.R.drawable.ic_menu_share);
    subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);    返回super.onCreateOptionsMenu(菜单);
}


解决方案

既然你已经使用onOptionsItemSelected,你有没有考虑处理所有的菜单项在那里呢?像这样的:

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    的setTitle(AndroidUtils.getTitle());    子菜单subMenu1 = menu.addSubMenu(NULL);    subMenu1.add(0,R.id.menu_save_loc,0,R.string.save_loc)
        .setIcon(android.R.drawable.ic_menu_save);
    subMenu1.add(0,R.id.menu_save_em,0,R.string.save_em)
        .setIcon(android.R.drawable.ic_dialog_email);    菜单项subMenu1Item = subMenu1.getItem();
    subMenu1Item.setIcon(android.R.drawable.ic_menu_share);
    subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
            | MenuItem.SHOW_AS_ACTION_WITH_TEXT);    返回super.onCreateOptionsMenu(菜单);
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    INT的itemId = item.getItemId();
    如果(==的itemId android.R.id.home){
        如果(mDrawerLayout.isDrawerOpen(mDrawerList)){
            mDrawerLayout.closeDrawer(mDrawerList);
        }其他{
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }
    否则,如果(==的itemId R.id.menu_save_loc){
        ...
    }
    否则,如果(==的itemId R.id.menu_save_em){
        ...
    }
    其他{
        返回super.onOptionsItemSelected(项目);
    }    返回true;
}

I am using NavigationDrawer with ActionBarSherlock. When I click on menu items onMenuItemClick() methods don't get called.

The code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {

        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {

            mDrawerLayout.closeDrawer(mDrawerList);
        } else {

            mDrawerLayout.openDrawer(mDrawerList);
        }
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    return super.onPrepareOptionsMenu(menu);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {

    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {

    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    setTitle(AndroidUtils.getTitle());
    SubMenu subMenu1 = menu.addSubMenu(null);
    subMenu1.add(getString(R.string.save_loc)).setIcon(android.R.drawable.ic_menu_save).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {

            ...
        }
    });
    subMenu1.add(getString(R.string.save_em)).setIcon(android.R.drawable.ic_dialog_email).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {

            ...
        }
    });
    MenuItem subMenu1Item = subMenu1.getItem();
    subMenu1Item.setIcon(android.R.drawable.ic_menu_share);
    subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return super.onCreateOptionsMenu(menu);
}
解决方案

Since you are already using onOptionsItemSelected, have you considered processing all of your menu items in there instead? Like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    setTitle(AndroidUtils.getTitle());

    SubMenu subMenu1 = menu.addSubMenu(null);

    subMenu1.add(0, R.id.menu_save_loc, 0, R.string.save_loc)
        .setIcon(android.R.drawable.ic_menu_save);
    subMenu1.add(0, R.id.menu_save_em, 0, R.string.save_em)
        .setIcon(android.R.drawable.ic_dialog_email);

    MenuItem subMenu1Item = subMenu1.getItem();
    subMenu1Item.setIcon(android.R.drawable.ic_menu_share);
    subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
            | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    if (itemId == android.R.id.home) {
        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }
    else if (itemId == R.id.menu_save_loc) {
        ...
    }
    else if (itemId == R.id.menu_save_em) {
        ...
    }
    else {
        return super.onOptionsItemSelected(item);
    }

    return true;
}

这篇关于onMenuItemClick不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 16:30