Which event is called when a user click on the disabled item in context menu?

风流意气都作罢 提交于 2019-12-02 02:03:10

问题


I have button, which displays a Context menu. In the menu are few items (some of them are disabled - setEnabled(false)).

Which event is called when a user click on the disabled item? It's not onContextItemSelected nor onContextMenuClosed. But the menu is closed after the click.

Thanks for your help.


回答1:


After consultation with my teacher, I've solved the problem. You can check the focus of your window, and then decide if the context menu was closed or not.

So you have to:

  1. Use the code below.
  2. Call onPrepareContextMenu() method when you create the context menu.

The code:

public class MyActivity extends android.app.Activity {

    private boolean contextMenuDisplayed = false;

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if(hasFocus && this.contextMenuDisplayed) {
            this.contextMenuDisplayed = false;
            this.onContextMenuClosed(null);
        }
    }

    public void onPrepareContextMenu() {
        this.contextMenuDisplayed  = true;
    }

}


来源:https://stackoverflow.com/questions/4045085/which-event-is-called-when-a-user-click-on-the-disabled-item-in-context-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!