Android custom menu and PopUpWindow pop-up menu

Source: Internet
Author: User

Use PopupWindow to create a custom menu. Add a sub-View to PopupWindow. The sub-View layout is the menu layout.

Animation that appears and exits: You can add an animation to the PopUpWindow or its child view.

All menus made with PopupWindow on the internet share a common feature: clicking the menu key shows a PopupWindow, and then clicking the menu key cannot cause PopupWindow to exit/dismiss ().

When setFocusable (true) is set for PopupWindow, after the menu is displayed, the menu disappears when you click anywhere else. However, the Click Event of the button does not respond at this time. At the same time, only the return key of the keyboard is returned. Other keys do not respond. For example, if you click the menu key, no response is made.

To solve this problem, set the following code for the sub-View of PopupWindow:


[Java]
<SPAN style = "COLOR: #993300; FONT-SIZE: 12px"> sub_view is a subview of PopupWindow.
Sub_view.setFocusableInTouchMode (true );
Sub_view.setOnKeyListener (new OnKeyListener (){
@ Override
Public boolean onKey (View v, int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_MENU) & (mPopupWindow. isShowing ())){
MPopupWindow. dismiss (); // enter the pop window that simulates the menu to exit.
Return true;
}
Return false;
}
}); </SPAN>

Sub_view is a subview of PopupWindow.
Sub_view.setFocusableInTouchMode (true );
Sub_view.setOnKeyListener (new OnKeyListener (){
@ Override
Public boolean onKey (View v, int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_MENU) & (mPopupWindow. isShowing ())){
MPopupWindow. dismiss (); // enter the pop window that simulates the menu to exit.
Return true;
}
Return false;
}
});


Android custom menu and PopUpWindow pop-up menu

Use PopupWindow to create a custom menu. Add a sub-View to PopupWindow. The sub-View layout is the menu layout.

Animation that appears and exits: You can add an animation to the PopUpWindow or its child view.

All menus made with PopupWindow on the internet share a common feature: clicking the menu key shows a PopupWindow, and then clicking the menu key cannot cause PopupWindow to exit/dismiss ().

When setFocusable (true) is set for PopupWindow, after the menu is displayed, the menu disappears when you click anywhere else. However, the Click Event of the button does not respond at this time. At the same time, only the return key of the keyboard is returned. Other keys do not respond. For example, if you click the menu key, no response is made.

To solve this problem, set the following code for the sub-View of PopupWindow:

[Java]
<SPAN style = "COLOR: #993300; FONT-SIZE: 12px"> // sub_view is a subview of PopupWindow.
Sub_view.setFocusableInTouchMode (true );
Sub_view.setOnKeyListener (new OnKeyListener (){
@ Override
Public boolean onKey (View v, int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_MENU) & (mPopupWindow. isShowing ())){
MPopupWindow. dismiss (); // enter the pop window that simulates the menu to exit.
Return true;
}
Return false;
}
}); </SPAN>

// Sub_view is a subview of PopupWindow.
Sub_view.setFocusableInTouchMode (true );
Sub_view.setOnKeyListener (new OnKeyListener (){
@ Override
Public boolean onKey (View v, int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_MENU) & (mPopupWindow. isShowing ())){
MPopupWindow. dismiss (); // enter the pop window that simulates the menu to exit.
Return true;
}
Return false;
}
});

 

Remember, you must set setFocusable (true) for PopupWindow. Otherwise, the menu will not exit when you click other menu and the Return key. In this case, the menu event of the parent of the PopupWindow is returned.

The following explains why PopupWindow exits after you click the menu key after the PopupWindow is displayed:

First, you must understand why PopupWindow setFocusable (true) does not respond after you click "menu.

When PopupWindow is initialized, it usually specifies the View on which it appears. We call this View as a parent. In the parent, the PopupWindow event appears when you click the menu. If you give PopupWindow setFocusable (true), the focus of the screen is on the PopupWindow, and it will certainly not respond to the parent button event, it only responds to the PopupWindow button event.

However, the essence of PopupWindow is Window, which does not inherit the View class and does not have onkeyDown, onkey, or dispatchKey events. At the beginning, I tried to implement these interfaces, but the buttons still did not respond. I don't know why. I cannot explain the reason because I am not familiar with the principle of buttons.

Then I want to bypass the line, that is, to register a key event for the sub-View of PopupWindow, setKeyListener. At the beginning, I set android in the sub-View xml: focusable = "true", but the button event still does not respond .... Tangle and tangle... There is no way. I google all the articles about PopupWindow... Finally, I found out that... Set setFocusableInTouchMode (true) for the sub-View of PopupWindow ). At this time, the key event will respond...

 

The complete code is attached below:

[Java]
<SPAN style = "COLOR: #993300; FONT-SIZE: 12px">/* must be overwritten. Otherwise, the onMenuOpened () must return false */
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
Menu. add ("menu"); // You must create an item.
Return super. onCreateOptionsMenu (menu );
}
/**
* Blocking MENU
*/
@ Override
Public boolean onMenuOpened (int featureId, Menu menu ){
If (mPopupWindow! = Null ){
If (! MPopupWindow. isShowing ()){
/* The most important step: the last two parameters displayed in the specified position (parent) are the coordinates relative to the x/y axis */
MPopupWindow. showAtLocation (findViewById (R. id. linear_menu_parent), Gravity. BOTTOM, 0, 0 );
}
}
Return false; // if the return value is true, the system menu is displayed.
}


Private void initPopuWindow (int menuViewID ){
LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService (LAYOUT_INFLATER_SERVICE );
/* Set the display menu Layout view sub-VIEW */
Sub_view = mLayoutInflater. inflate (menuViewID, null );
/* The first parameter is displayed. The last two parameters are the window size */
MPopupWindow = new PopupWindow (sub_view, LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT );
/* Set the background display */
MPopupWindow. setBackgroundDrawable (getResources (). getDrawable (R. drawable. bg_menu_popup ));
/* Set to disappear when you touch the outside */
MPopupWindow. setOutsideTouchable (true );
/* Set system animation */
MPopupWindow. setAnimationStyle (android. R. style. Animation_Dialog );
MPopupWindow. update ();
MPopupWindow. setTouchable (true );
/* Set to click other places outside menu and return key to exit */
MPopupWindow. setFocusable (true );

/** 1. Solve the Problem of no response when you click the MENU key again
* 2. sub_view is a subview of PopupWindow.
*/
Sub_view.setFocusableInTouchMode (true );
Sub_view.setOnKeyListener (new OnKeyListener (){
@ Override
Public boolean onKey (View v, int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_MENU) & (mPopupWindow. isShowing ())){
MPopupWindow. dismiss (); // enter the pop window that simulates the menu to exit.
Return true;
}
Return false;
}
});


/* Listen to MENU events */
Menu = new View [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 (new OnClickListener (){
@ Override
Public void onClick (View v ){
// DoSomething

}
});

Menu [1]. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// DoSomething

}
});

Menu [2]. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// DoSomething

}
});
} </SPAN>

/* It must be overwritten. Otherwise, if the MENU is not displayed, the onMenuOpened () must return false */
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
Menu. add ("menu"); // You must create an item.
Return super. onCreateOptionsMenu (menu );
}
/**
* Blocking MENU
*/
@ Override
Public boolean onMenuOpened (int featureId, Menu menu ){
If (mPopupWindow! = Null ){
If (! MPopupWindow. isShowing ()){
/* The most important step: the last two parameters displayed in the specified position (parent) are the coordinates relative to the x/y axis */
MPopupWindow. showAtLocation (findViewById (R. id. linear_menu_parent), Gravity. BOTTOM, 0, 0 );
}
}
Return false; // if the return value is true, the system menu is displayed.
}
 

Private void initPopuWindow (int menuViewID ){
LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService (LAYOUT_INFLATER_SERVICE );
/* Set the display menu Layout view sub-VIEW */
Sub_view = mLayoutInflater. inflate (menuViewID, null );
/* The first parameter is displayed. The last two parameters are the window size */
MPopupWindow = new PopupWindow (sub_view, LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT );
/* Set the background display */
MPopupWindow. setBackgroundDrawable (getResources (). getDrawable (R. drawable. bg_menu_popup ));
/* Set to disappear when you touch the outside */
MPopupWindow. setOutsideTouchable (true );
/* Set system animation */
MPopupWindow. setAnimationStyle (android. R. style. Animation_Dialog );
MPopupWindow. update ();
MPopupWindow. setTouchable (true );
/* Set to click other places outside menu and return key to exit */
MPopupWindow. setFocusable (true );

/** 1. Solve the Problem of no response when you click the MENU key again
* 2. sub_view is a subview of PopupWindow.
*/
Sub_view.setFocusableInTouchMode (true );
Sub_view.setOnKeyListener (new OnKeyListener (){
@ Override
Public boolean onKey (View v, int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_MENU) & (mPopupWindow. isShowing ())){
MPopupWindow. dismiss (); // enter the pop window that simulates the menu to exit.
Return true;
}
Return false;
}
});


/* Listen to MENU events */
Menu = new View [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 (new OnClickListener (){
@ Override
Public void onClick (View v ){
// DoSomething

}
});

Menu [1]. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// DoSomething

}
});

Menu [2]. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// DoSomething

}
});
}

 

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.