简体   繁体   English

单击菜单后打开上下文菜单栏

[英]Open a contextual menu bar upon menu click

I'm wondering if there is a way to open a contextual menu bar upon click on the phones menu button in Android. 我想知道是否有一种方法可以通过在Android中单击电话菜单按钮来打开上下文菜单栏。 I create a contextual menu bar according to this basic tutorial: tutorial contextual menu bar 我根据此基础教程创建了一个上下文菜单栏: 教程上下文菜单栏

And I have now the following in my Activity: 现在,我的活动中包含以下内容:

mCallback = new Callback(){

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch(item.getItemId()){
        case 1:
            Toast.makeText(getBaseContext(), "Selected Action1 " + postId, Toast.LENGTH_SHORT).show();
            //mode.finish();    // Automatically exists the action mode, when the user selects this action

            break;
        case 2:
            Toast.makeText(getBaseContext(), "Selected Action2 "  + postId, Toast.LENGTH_SHORT).show();
            //mode.finish();    // Automatically exists the action mode, when the user selects this action
            break;
        case 3:
            Toast.makeText(getBaseContext(), "Selected Action3 " + postId, Toast.LENGTH_SHORT).show();
            //mode.finish();    // Automatically exists the action mode, when the user selects this action
            break;

        }
        return false;

    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.setTitle("Actions for Post");
        getMenuInflater().inflate(R.menu.context_menu_posts, menu);

        menu.add(1, 1, 1, "Option 1");
        menu.add(1, 2, 2, "Option 2");
        menu.add(1, 3, 3, "Option 3");



        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mMode = null;

    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

};

But now I want it to "show" when the user clicks the menu button. 但是现在我希望它在用户单击菜单按钮时“显示”。 Before I had this code in a LongclickListener: 在LongclickListener中使用此代码之前:

if(mMode!=null)
        return false;
    else
        mMode = startActionMode(mCallback);

This worked great for a control in the Activity but is there a way to do this for a menu click? 这对于“活动”中的控件非常有用,但是是否可以通过菜单单击来做到这一点? I tried putting the code for the startActionMode in the @Overriden menu methods but I can't get it to open upon click on the menu button. 我尝试将startActionMode的代码放入@Overriden菜单方法中,但单击菜单按钮后无法打开它。

Thanks in advance! 提前致谢!

Sorry for stupid post, I must be more tired than I thought. 对不起,我很笨,我一定比我想的还要累。

Wasn't thinking that it is just to Override the menubutton and add the code there, since I'm not using that before. 因为我以前没有使用过,所以没有想到只是覆盖menubutton并在其中添加代码。

This Post solved it for me: Intercept menuclick 这篇文章为我解决了: 拦截menuclick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM