problems encountered when doing custom pop-up windows, if you do not inherit dialog or Popwindow write by GetWindow Decorview to Decorview view dynamic add view and remove view to implement pop -up window The return button will appear. If you need to close the pop-up window (that is, remove view), then how to listen to the return button? Here we see dialog and Popwindow found the law of writing, that both have achieved window.callback, and Keyevent.callback, there are three implementations of Keyevent.callback here:
OnKeyUp: Lift Up
Onkeylongpress: Long Press
OnKeyDown: Press the
And Window.callback, it is more, here I do not explain, mainly say two places:
Dispatchkeyevent: Distributing key events (this is the home keyboard, or the return key, and the volume keys, power keys, etc. are key events)
Dispatchtouchevent: Distributing touch events, here is the distribution of touch events
If you want to intercept the return button of the cell phone, you need to let Windows window handle callback here when the popup is displayed.
That is window.setcallback (); it's your own callback, or it's always been the activity. Handling keystrokes, as well as touching events
1. At show time Activity.getwindow (). Setcallback (this) own implementation
2. Then rewrite the dispatchkeyevent to let the processing distribute
@Override publicboolean dispatchkeyevent (KeyEvent event) { If (Xwindow.superdispatchkeyevent (event)) { returntrue; } return event.dispatch (This isnull nullthis); }
3. After the distribution, the onkeydown Press of the Keyevent.callback event
@Override Public boolean onKeyDown (int keycode, KeyEvent event) { if (keycode = = Keyevent.keycode_back) { event.starttracking (); return true ; } return false ;}
4. Press the event after processing is lifted, here to determine if the current display, close the pop-up window (remove view), and then consume the event
@Override public boolean onKeyUp (int KeyCode, KeyEvent event) { if (keycode = = Keyevent.keycode_back && event.istracking () &&! event.iscanceled ()) { if (isshowing) {dismiss (); return true ; return false ; return false
5. After closing the pop-up window, don't forget to return the form callback to the activity's callback
Xwindow.setcallback (Activity);
6. So far the key is done, but there is a problem, callback not only handle the keys, but also handle the touch events, if not to do the processing
Then the touch on the screen is invalid, no matter how the touch is not responding, mainly because
Dispatchtouchevent did not do the processing, here also need to do:
@Override Public Boolean dispatchtouchevent (Motionevent event) { if (Xwindow.superdispatchtouchevent (event)) { return true ; } return false ;}
We're all done here.
The code Xwindow variable above is the Window object retrieved from the Activity Gewindow ()
Android Monitor touch events, and various key handling