Popupwindow advanced usage-implement a custom menu similar to ucweb on Android to fully simulate system events

Source: Internet
Author: User

The system menu has three events:

    1. Click other menu items to exit
    2. Click Back to exit
    3. Click menu to exit

The source code of the custom menu that imitates ucweb is available on the Internet. Below are three classicArticle:

    1. Android: Improving customized menu (tabmenu) in 18 articles)
    2. [Source code release] releases the latest menu source code of the ucweb7.4 interface ~!
    3. Imitating ucweb menus

All three are made using popupwindow, and then add a sub-view to popupwindow. The sub-view layout is the menu layout.

Animation that appears and exits: You can give a popupwindow or its sub-view. For these questions, see the source code of the above three examples.

All menus made with popupwindow on the internet share a common feature: Click the menu key to display the popupwindow, and then click the menu key to make it unavailable.
Popupwindow exit/dismiss (). Some even click the button to display the menu, and click the event button to write the button at this time. The menu will
ExitedCode.

Why do we do that? Because when setfocusable (true) is set for popupwindow, after the menu is displayed, click
And menu will disappear. That is to say, the button clicking event 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:

<textarea cols="50" rows="15" name="code" class="java">// Custom_menu is the sub-view of popupwindow <br/> custom_menu.setfocusableintouchmode (true); <br/> custom_menu.setonkeylistener (New onkeylistener () {<br/> @ override <br/> Public Boolean onkey (view V, int keycode, keyevent event) {<br/> If (keycode = keyevent. keycode_menu) & (isshowing () {<br/> dismiss (); // enter the pop window of the simulated menu to exit. <br/> return true; <br/>}< br/> return false; <br/>}< br/> });</textarea>

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 need to understand why to give popupwindow
After setfocusable (true), click menu to see why popupwindow does not respond. Popupwindow initialization time 1
Specifies the view on which the view appears. We call this view as a parent. In the parent, the popupwindow event appears when you click menu.
Popupwindow
Setfocusable (true). At this time, the screen focus is on the popupwindow. It certainly does not respond to the parent button event, but only responds
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, register the key event and setkeylistener for the sub-view of popupwindow. At the beginning, I set the key event in the sub-view XML.
Android: 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... To
Set setfocusableintouchmode (true) for the sub-view of popupwindow ). At this time, the key event will respond...

Above what did not understand the place, can leave a message to propose (Message link: http://www.girlcoding.com/2011/04/popupwindow-custom-menu/
).

Original article, reprinted Please note:
Reproduced from girl is coding

Link:
Android custom menu, fully Simulating System Events

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.