开发者

Show context menu from code behind

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 开发者_C百科call the context menu?


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


 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);


you can use any of the following:

  1. openContextMenu as shown here:
registerForContextMenu(view); 
openContextMenu(view);
unregisterForContextMenu(view);
  1. setOnCreateContextMenuListener

  2. showContextMenuForChild


You can use

view.showContextMenu();

on your view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜