dismiss popup when options menu is pressed

老子叫甜甜 提交于 2019-12-11 08:09:26

问题


I implemented a popup which shows up when I press the hardware menu key. Now I want to dismiss the popup when I press on the menu key again.

I tried this

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (pw.isShowing()) {
            pw.dismiss();
        } else {
            openpopup();
        }
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

here's the open popup method

public void openpop() {
    View view1 = getCurrentFocus();
    showpopup(view1);
}

here's the showpopup method

public void showpopup(View view) {

    pw.setTouchable(true);
    pw.setFocusable(true);
    pw.setTouchInterceptor(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                pw.dismiss();

                return true;
            }

            return false;
        }
    });

    try {
        pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        pw.setOutsideTouchable(false);
        pw.setContentView(popupView);
        pw.showAsDropDown(view, 0, 0);
        popUpShowed = true;
    } catch (Exception e) {
        Log.e("SAMPLE", "Exception", e);
    }

}

But the popup is not dismissed when I press menu.

What could be the problem? I need to get this done somehow. Please help me out.


回答1:


Apparently, pressing Menu Key second time does not trigger.
Found here the solution.
Hope it helps.

EDIT :
Checked and created another approach. This should do it :

boolean open = false;
boolean itemSelected = false;

@Override
public boolean onMenuOpened(int featureId, Menu menu) {

    if(!open || itemSelected) {
        showPopup();
        open = true;
        itemSelected = false;
    }
    return false;
}

@Override
public void onPanelClosed(int featureId, Menu menu) {
    if (open && !itemSelected) {
        closePopup();
        open = false;
    }
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    itemSelected = true;
    return false;
}

Works for me on Android 4.0.3. Apparently onPrepareOptionsMenu has changed functionality after v3.




回答2:


Why don't you do this ?

private boolean popUpShowed = false;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (popUpShowed) {
            pw.dismiss();
            popUpShowed = false;
        } else if(!popUpShowed){
            openpopup();
            popUpShowed = true;
        }
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

And delete the keyListener on your popup.




回答3:


try this:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 

if (keyCode == KeyEvent.KEYCODE_MENU) { 
    if (pw.isShowing()) { 
        closePW(); 
    } else { 
        openpopup(); // <- I think you do this already here
    } 
    return true; 
} 

return super.onKeyDown(keyCode, event); 
} 
}

// Put this function in the class which open the popup
// like your function openpopup()
public void closePW(){
   pw.dismiss();
}


来源:https://stackoverflow.com/questions/11946797/dismiss-popup-when-options-menu-is-pressed

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