Solution of Android popupwindow clicking the external and return keys to disappear
When we first took over popupwindow, we may all think it's very simple, because it's really simple, but if we're unlucky, we may step into a pit:
Click the outermost layout of popupwindow and click the back key. Popupwindow will not disappear
Novices may toss around for a long time when they encounter this problem, and finally find a solution through a powerful network, that is, setting a background with popupwindow
Popupwindow. Setbackgrounddrawable (drawable). Any type of drawable can be used as long as it is not empty.
Demo address: smartpopupwindow_ jb51.rar
Let's see what happened to the pop-up box where the return key cannot disappear from the source code (I see android-22):
Let's take a look at the code showasdropdown when the pop-up box is displayed. There is a preparepopup method in it.
Look at the preparepopup method
It can be seen from the above that when the mbackground is not empty, the popupviewcontainer will be used as the parent of the mcontentview. Let's see what the popupviewcontainer does
As can be seen from the red part above, this internal class encapsulates the logic for processing the return key exit and clicking the external exit, but there is an mbackground during the construction of this class object (in the preparepopup method)= Null conditions will be created
The mbackground object is assigned in the setbackgrounddrawable method. You should understand everything here.
In addition to being called externally, the setbackgrounddrawable method will also be called in the constructor. It is taken from system resources by default
Some versions do not. Android 6.0 preparepopup is as follows:
The code to implement return key listening here is mdecorview = createdcorview (mbackgroundview). This is not controlled by the mbackground variable, so this version should not have the problems we described. If you are interested, you can try it yourself
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.