Click outside to disappear, only need to set popupwindow.setbackgrounddrawable (new Paintdrawable ());
Set up
Popupwindow.setfocusable (TRUE);
Popupwindow.setbackgrounddrawable (New paintdrawable ());
Popupwindow.setoutsidetouchable (TRUE);
Popupwindow_view.setfocusableintouchmode (True); or click outside to disappear
Response to the back key disappears relatively simple, in the Popview plus Setonkeylistener event, determine whether the button is back on it
But the response to the menu key is troublesome. Click the menu key appears popwindows and click menu to respond to the location of the first response is not the same, there is always a focus conflict. Here's a better solution.
The content is transferred from http://blog.csdn.net/admin_/article/details/7278402
Use Popupwindow to customize the menu, add a sub-view to Popupwindow, and the layout of the child view is the menu layout.
Animations that appear and exit: can be added to Popupwindow or to its child view.
All the online menu with Popupwindow has a common feature: is to click the menu key appears Popupwindow, and then click the menu key cannot make Popupwindow exit/dismiss ().
When Popupwindow is set to Setfocusable (true), menu is displayed, click anywhere else, menu will disappear, but the button's Click event is not responding. At the same time, only respond to the keyboard return key, other keys are not responding, such as clicking the menu button, no response.
To solve this problem, it is simple to set the following code for Popupwindow's Child view:
//Sub_view is Popupwindow's child view.Sub_view.setfocusableintouchmode (true); Sub_view.setonkeylistener (NewOnkeylistener () {@Override Public BooleanOnKey (View V,intKeyCode, KeyEvent event) { //TODO auto-generated Method Stub if((keycode = = Keyevent.keycode_menu) &&(Mpopupwindow.isshowing ())) {Mpopupwindow.dismiss ();//here, the Popupwindow of the analog menu exits . return true; } return false; } });
Remember, be sure to set setfocusable (true) for Popupwindow, or menu will not exit if you click elsewhere in the menu and the return key. And this is the menu event that responds to the parent of Popupwindow.
The following explains why after this is written, when Popupwindow is displayed, click the Menu key Popupwindow will exit the reason:
First you have to understand why give Popupwindow setfocusable (True), click menu appears Popupwindow and then click Menu did not respond to the reason.
When the Popupwindow is initialized, it usually specifies which view to appear on, and we call this view the parent. The parent has written the Click menu to appear Popupwindow event, if give Popupwindow setfocusable (True), at this time the focus of the screen on the Popupwindow, certainly will not respond to the key events of the parent, It will only respond to Popupwindow key events.
But the essence of Popupwindow is window, without inheriting the view class, without onkeydown or OnKey or dispatchkey these events. I just started trying to implement these interfaces, but the keys are still not responding and I don't know why. Because now the principle of the key is not ripe, unable to explain its reasons.
Then I want to make a detour, that is, to Popupwindow's Child View Registration key event, Setkeylistener, just beginning I in the child view of the XML set android:focusable= "true" but the key event is still not responding .... Tangled, Tangled ... Then there is no way, I google all the articles about Popupwindow ... Finally I found ... You need to set Setfocusableintouchmode (true) for Popupwindow's child view. The button event responds ...
The complete code is attached below:
/*must be rewritten, otherwise click menu unresponsive in order to let him not show, below onmenuopened () must return False*/@Override Public BooleanOncreateoptionsmenu (Menu menu) {menu.add ("Menu");//You must create an item return Super. Oncreateoptionsmenu (menu); } /*** Block Menu*/@Override Public BooleanOnmenuopened (intFeatureid, Menu menu) { if(Mpopupwindow! =NULL){ if(!mpopupwindow.isshowing ()) { /*The most important step: pop-up display at the specified position (parent) The last two parameters are the coordinates relative to the X/Y axis*/mpopupwindow.showatlocation (Findviewbyid (r.id.linear_menu_parent), Gravity.bottom,0, 0); } } return false;//returns True to display the system menu } Private voidInitpopuwindow (intMenuviewid) {Layoutinflater Mlayoutinflater=(Layoutinflater) Getsystemservice (Layout_inflater_service); /*Set Display menu Layout view Sub View*/Sub_view= Mlayoutinflater.inflate (Menuviewid,NULL); /*the first parameter pops up to show the view after the two are window sizes*/Mpopupwindow=NewPopupwindow (Sub_view, layoutparams.fill_parent, layoutparams.wrap_content); /*Setting the background display*/mpopupwindow.setbackgrounddrawable (Getresources (). getdrawable (R.drawable.bg_menu_popup)); /*set to disappear when touch is outside*/mpopupwindow.setoutsidetouchable (true); /*set up system animations*/Mpopupwindow.setanimationstyle (Android. R.style.animation_dialog); Mpopupwindow.update (); Mpopupwindow.settouchable (true); /*settings Click anywhere other than menu and return key to exit*/mpopupwindow.setfocusable (true); /**1. Solve the problem by clicking the menu key again * 2.sub_view is Popupwindow's child view*/Sub_view.setfocusableintouchmode (true); Sub_view.setonkeylistener (NewOnkeylistener () {@Override Public BooleanOnKey (View V,intKeyCode, KeyEvent event) { //TODO auto-generated Method Stub if((keycode = = Keyevent.keycode_menu) &&(Mpopupwindow.isshowing ())) {Mpopupwindow.dismiss ();//here, the Popupwindow of the analog menu exits . return true; } return false; } }); /*Listening for menu events*/Menu=NewView[3]; menu[0] =Sub_view.findviewbyid (R.ID.MENU_0); menu[1] =Sub_view.findviewbyid (r.id.menu_1); menu[2] =Sub_view.findviewbyid (r.id.menu_2); menu[0].setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//dosomething } }); menu[1].setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//dosomething } }); menu[2].setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//dosomething } }); }
Online has a copy of the UCWeb custom menu source code, here are three more classic articles:
- Android Improved 18 custom menu (Tabmenu)
- "Source release" Imitation UCWEB7.4 interface the latest menu source code out ~!
- Mimic UCWeb Menu