Let's take a look at the Android API's Methods:
Public void setOutsideTouchable (boolean touchable) Controls whether the pop-up will be informed of touch events outside of its window. this only makes sense for pop-ups that are touchable but not focusable, which means touches outside of the window will be delivered to the window behind. the default is false.
If the popup is showing, calling this method will take effect only the next time the popup is shown or through a manual call to one ofupdate()
Methods.
Parameters
Touchable |
True if the popup showould receive outside touch events, false otherwise |
See Also
isOutsideTouchable()
isShowing()
update()
That is to say, by using this attribute and setFocusable (true); you can click another region to make popup disappear,
You can also do this by clicking the popup form itself to make it disappear. Use the following method to rewrite the onTouch method as root:
// Click the form and the PopupWindow disappears as root. setOnTouchListener (new View. onTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {popup. dismiss (); return true ;}});
Similarly, you do not need to override the root onTouch method. Instead, you can rewrite the onTouchEvent () method of the Activity to disappear popup normally.
@Override public boolean onTouchEvent(MotionEvent event) { if (popup != null && popup.isShowing()) { popup.dismiss(); popup= null; } return super.onTouchEvent(event); }
However, I encountered a problem that none of the above methods can be solved. Why?
In fact, the culprit is the code sequence of popup. The Shouba first calls the showAsDropDown () method and sets other attributes, which leads to this situation.
The showAsDropDown method is equivalent to the Dialog. show () method. If it is show first, other attributes do not play an appropriate role even if they are set.
This often reminds others that I made the same mistake in popup.