Click the menu key popupWindow to display and disappear.

Source: Internet
Author: User

Click the menu key popupWindow to display and disappear.


Reprinted please indicate the source, thank you http://blog.csdn.net/harryweasley/article/details/45217273

Recently, I want to implement this function: Click the menu key, popupWindow is displayed, and then click the menu key. popupWindow disappears. You can also click the normal button to make popupWindow appear and disappear.


I'm talking about whether it's a bit messy. Let's take a look at the specific effect, just like this ,:


This popupWindow can be displayed either by clicking the "+" or by clicking the menu key. Of course, you can also click the screen or the menu key to disappear.


It's easy to click "+ ".

right.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {showPopupWindow();}});



In order to click the menu key to display popupWindow, We need to rewrite this code under Activity and click the menu key to determine whether popupWindow is displayed. Otherwise, the display disappears.


@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_MENU:if (popupWindow != null && popupWindow.isShowing()) {popupWindow.dismiss();} else {showPopupWindow();}break;}return super.onKeyDown(keyCode, event);}



The method in showPopupWindow () is as follows.


/*** Display popupWindow */private void showPopupWindow () {// TODO the code for displaying popupWindow is LayoutInflater inflater = (LayoutInflater) getSystemService (Context. LAYOUT_INFLATER_SERVICE); view = inflater. inflate (R. layout. popupwindow, null); int width = getWindow (). getDecorView (). getWidth ()/3; Log. I ("width", width + ""); popupWindow = new PopupWindow (view, width, WindowManager. layoutParams. WRAP_CONTENT); popupWindow. setFocusable (true); // popupWindow. setBackgroundDrawable (new BitmapDrawable (); ColorDrawable dw = new ColorDrawable (-00000); popupWindow. setBackgroundDrawable (dw); popupWindow. showAsDropDown (MainActivity. this. findViewById (R. id. right),-dip2px (MainActivity. this, 100), 0 );}

After using the above method, you will find a problem. When I click the menu key popupWindow to pop up, but when I click the menu key again, the popupWindow will not disappear, and only the screen can be clicked to disappear.


This is obviously not the expected effect. After popupWindow is displayed, it does not respond when you click the menu key again because popupWindow gets the focus when it is displayed, the onKeyDown (int keyCode, KeyEvent event) in the Activity will not be responded, that is, the Activity's key event will not respond.


In this case, you can click the menu key to display popupWindow. We can obtain a subview of popupWindow (I am the outermost LinearLayout) and set setOnKeyListener, let this view respond to the menu button event when the popupWindow is displayed. The above two codes are changed to the following:


@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_MENU:isMenuShow = true;showPopupWindow();break;}return super.onKeyDown(keyCode, event);}



/*** Display popupWindow */private void showPopupWindow () {// TODO the code for displaying popupWindow is LayoutInflater inflater = (LayoutInflater) getSystemService (Context. LAYOUT_INFLATER_SERVICE); view = inflater. inflate (R. layout. popupwindow, null); LinearLayout ll = (LinearLayout) view. findViewById (R. id. line); int width = getWindow (). getDecorView (). getWidth ()/3; Log. I ("width", width + ""); popupWindow = new PopupWindow (view, Width, WindowManager. layoutParams. WRAP_CONTENT); popupWindow. setFocusable (true); ll. setFocusableInTouchMode (true); ll. setOnKeyListener (new OnKeyListener () {@ Overridepublic boolean onKey (View v, int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_MENU) {if (! IsMenuShow) {popupWindow. dismiss () ;}ismenushow = false ;}return false ;}}); // popupWindow. setBackgroundDrawable (new BitmapDrawable (); ColorDrawable dw = new ColorDrawable (-00000); popupWindow. setBackgroundDrawable (dw); popupWindow. showAsDropDown (MainActivity. this. findViewById (R. id. right),-dip2px (MainActivity. this, 100), 0 );}




I added a boolean


/*** Click the menu key to open popupWindow. The default value is false */boolean isMenuShow = false;

This must be added. Do not cause this problem. When you click the menu key, popupWindow will disappear immediately, because the two menu events above have corresponding values.


Now I want to see the effect. Hey, it's good ~~~~

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.