ActionBarSherlock:打开子菜单的菜单按钮 [英] ActionBarSherlock: open submenu with menu button

查看:262
本文介绍了ActionBarSherlock:打开子菜单的菜单按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ActionBarSherlock-4.1.0-0,我想开我的子菜单中的动作条与硬件菜单按钮。我刨的更新和我的老版本,我用了正常的菜单。我想,以帮助用户来适应新的设计。 我得到的子菜单,主菜单:

I'm using ActionBarSherlock-4.1.0-0 and I would like to open my submenu in the Actionbar with the hardware menu button. I'm planing an update and in my old version I used the "normal" menu. I'd like to help users to get used to the new design. I got the submenu and the main-menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater i = getSupportMenuInflater();
    i.inflate(R.menu.main_menu, menu);
    SubMenu subMenu = (SubMenu) menu.findItem(R.id.actionbar_submenu);
    Menu mainMenu = menu;
    return super.onCreateOptionsMenu(menu);
}

和我有一个听者的硬件菜单按钮:

and i got a listner to the hardware menu button:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode) {
        case KeyEvent.KEYCODE_MENU:
            // TODO: expand submenu from actionbar
            return true;

        }
    }
    return super.onKeyDown(keyCode, event);
}

我无法找到一个方法或其他任何来电。我希望你能帮助我, 干杯, 保

I couldn't find a method or anything else to call. I hope you can help me, cheers, Paul

推荐答案

我尝试这个解决方案由弗雷德里克与Android动作条,我碰到了子菜单打开,并立即关闭问题。更改为的onkeyup解决了这个问题。

I try this solution from Frederik with android actionbar and I run into the problem that the submenu opens and closes immediately. Changing to onKeyUp solved this issue.

下面是我的code:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_MENU){
        if (event.getAction() == KeyEvent.ACTION_DOWN && optionsMenu != null && optionsMenu.findItem(R.id.sub_menu) != null)
        {
            Log.i(TAG, "performIdentifierAction");
            optionsMenu.performIdentifierAction(R.id.sub_menu, 0);
            return true;
        }
    }
    return super.onKeyUp(keyCode, event);
}

我做检查,如果 optionsMenu = NULL和放大器;!&安培; optionsMenu.findItem(R.id.sub_menu)!= NULL ,因为与旧的Andr​​oid版本没有动作条的兼容性问题。如果你使用ActionBarSherlock的所有版本这不是所必要的。

I do check if optionsMenu != null && optionsMenu.findItem(R.id.sub_menu) != null because of compatibility issues with older Android versions without actionbar. This isn't nessesary if you use ActionBarSherlock for all versions.

这篇关于ActionBarSherlock:打开子菜单的菜单按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆