ActionbarSherlock open submenu on device menu on k

2019-07-19 00:28发布

问题:

I make action bar sherlock appliction and open for submenu on top bar actionbar shelock on click and shown but on device menu click not open for submenu inside for actionbar shelock kindly help me.

@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    com.actionbarsherlock.view.SubMenu subMenu = menu.addSubMenu("Menu");
    subMenu.add(0, SUB_MENU_1, 0,"Settings");
    com.actionbarsherlock.view.MenuItem menuItem = subMenu.getItem();
    menuItem.setIcon(R.drawable.menu_selector);
    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return true;
}

Here i m cofuse for open submenu for action bar sherlock on key down,

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_MENU:
                        // here some write code
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

Thanks in advance,

回答1:

I found for my self solution for this question,

public Menu menuMain;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menuMain = menu;
    subMenu = menu.addSubMenu(0, MENU_ID_1, 0, "Menu");
    subMenu.add(0, SUB_MENU_1, 0, getString(R.string.menu_settings));
    subMenu.add(0, SUB_MENU_2, 0, getString(R.string._new));
    menuItem = subMenu.getItem();
    menuItem.setIcon(R.drawable.menu_selector);
    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return true;
}

First We Global variable show on actionbar sherlock menu and initialize for onCreateOptionMenu,

And onkeyup override method for activity define,

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_UP || event.getAction() == KeyEvent.ACTION_DOWN) {

        switch (keyCode) {
        case KeyEvent.KEYCODE_MENU:
            if (menuMain != null && menuMain.findItem(MENU_ID_1) != null)
                menuMain.performIdentifierAction(MENU_ID_1, 0);
            return true;
        }
    }
    return false;
}

So, be happy its working very fine and no such any exception throws....!!!!!!