Solution for Android popupwindow to disappear when the focus is lost or when a blank area is clicked

Source: Internet
Author: User

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.