Android 从后面的代码显示上下文菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3695827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:24:56  来源:igfitidea点击:

Show context menu from code behind

androidcontextmenushowcode-behind

提问by AnD

This might be a simple question, but I've been looking around and can't find the answer. Is there any code to show the context menu on Android from a code, instead of pressing the menu button? E.g. when I touch the screen then it'll call the context menu?

这可能是一个简单的问题,但我一直环顾四周,找不到答案。是否有任何代码可以从代码中显示 Android 上的上下文菜单,而不是按下菜单按钮?例如,当我触摸屏幕时,它会调用上下文菜单?

回答by CommonsWare

Call openContextMenu()on your Activitywhenever you want to open it. Note that this is a rather unusual UI pattern, one that your users may not expect.

想打开它时随时打电话openContextMenu()给您Activity。请注意,这是一种相当不寻常的 UI 模式,您的用户可能不会想到这种模式。

回答by hrules6872

 OnClickListener onClick_Show_Contextmenu = new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) context).openContextMenu(v);
            }

        };

        findViewById(R.id.xxx).setOnClickListener(onClick_Show_Contextmenu);

        registerForContextMenu(findViewById(R.id.xxx));
        findViewById(R.id.xxx).setLongClickable(false);

回答by android developer

you can use any of the following:

您可以使用以下任何一种:

  1. openContextMenuas shown here:
  1. openContextMenu如下所示:
registerForContextMenu(view); 
openContextMenu(view);
unregisterForContextMenu(view);
registerForContextMenu(view); 
openContextMenu(view);
unregisterForContextMenu(view);
  1. setOnCreateContextMenuListener

  2. showContextMenuForChild

  1. setOnCreateContextMenuListener

  2. showContextMenuForChild

回答by B-GangsteR

You can use

您可以使用

view.showContextMenu();

on your view.

在你看来。