问题
I want to dismiss the pop up window when back button is pressed. I tried with this code:
popwindow.setBackgroundDrawable(new BitmapDrawable());
and it works. But in my app, pop should remain, even after touching outside of the pop up window. It should be dismissed only when back button is pressed. So I tried this: popwindow.setFocusable(false);
Now its not dismissing when touched outside the pop up. But its not dismissing on back press too. I do not want to overide `onBackPressed(). Is there any other way, through which i can achieve this. Thanks in advance..
回答1:
To disable pop up dismiss when click outside of popwindow set
popwindow.setCanceledOnTouchOutside(false);
and for dismiss it on back button set
popwindow.setCancelable(true);
回答2:
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
new ScaleInAnimation(popupView).animate();
popupWindow.getContentView().setFocusableInTouchMode(true);
popupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
popupWindow.dismiss();
return true;
}
return false;
}
});
回答3:
Set like this..
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setTouchInterceptor(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (AppContext.isDebugMode())
Log.d("POPUP_WINDOW", "v: "+v.getTag() + " | event: "+event.getAction());
popupWindow.dismiss(); return true;
}
});
回答4:
Aside from the setOutsideTouchable(true)
and setFocusable(true)
I had to add
popUpView.setBackgroundDrawable(new BitmapDrawable())
to make it work. This did not change the UI of my popup, but for some magical reason, enabled the back button functionality.
回答5:
The code below may be helpfull:
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(mParent);
And,the show of the popupWindow (popupWindow.showAsDropDown or popupWindow.showAtLocation )must be called in the end.
回答6:
Set Popup property as..
popup.setOutsideTouchable(false);
popup.setCancelable(true);
来源:https://stackoverflow.com/questions/25256221/dismiss-pop-up-window-when-back-button-is-pressed