开发者

How to get the contextview by clicking on the option menu

Hal开发者_如何学Co,

My ideia is to show a Context Menu by clicking on one of the options menu.

I don't think it will be quite difficult but I'm not being able to get the View in the OptionsMenu class.

So actually it is just call the showContextMenu() on the OptionsMenu class.

Can somebody give me a hint?

ty!


It's possible to call a Context Menu by means other than a long press. If you implement your menu options within your activity, using an inner class for instance, you should be able to call your context menu:

yourView.showContextMenu();

Remember to register your context menu at onCreate:

registerForContextMenu(yourView);


Finally made it, and it was a quite simple solution.

Basically I pass the View, to the OptionsMenu constructor.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.setContentView(R.layout.activity_event);

    View v = findViewById(R.id.activity_event);

    registerForContextMenu(v);

    options = new Options(getApplicationContext(), getMenuInflater(), v);

    fillData(); 
}

...

   private class Options extends ActivityOptionsMenu {
        public Options(Context c, MenuInflater mi, View v) {
           super(c, mi, v);
         }
   }

...

     private abstract class ActivityOptionsMenu {
     ...

         public void onMenuItemSelected(int featureId, MenuItem item) {
            switch (item.getItemId()) {
            case R.id.directions:
                v.showContextMenu();
            }
         }
   }

Thanks for all the help.


My ideia is to show a Context Menu by clicking on one of the options menu.

AFAIK, you cannot show a context menu from a long-tap on an option menu choice.

I don't think it will be quite difficult but I'm not being able to get the View in the OptionsMenu class.

The View is not exposed to you.

The only way to literally do what you seek is to not use the standard Android option menu. You can watch for the MENU key via onKeyDown() in your activity, at which point you can display whatever you want, including something that supports context menus.

However, context menus are not particularly popular among users, simply because they are not very discoverable. Users never read the documentation, even if it is supplied, and they tend not to randomly stab the screen to see if a menu will pop up. They will be even less likely to decide to hold down a fake option menu choice to see if a context menu will pop up.

Hence, I really recommend you consider some other approach, for improved usability.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜