The Android-menu menu uses a

Source: Internet
Author: User

    • Create a Menu

   In Androidsdk, you do not need to create a menu object from scratch. Because an activity is associated with only one menu, Android creates this menu for the activity and then passes it to the oncreateoptionsmenu callback of the activities class. This method can be used to populate a single incoming menu with a set of menu items.

@Override      Public Boolean Oncreateoptionsmenu (Menu menu) {        //....        return Super . Oncreateoptionsmenu (menu);    }

When the menu item is populated, this code returns TRUE to make the menu visible. If you return false, the menu will not be visible.

  @Override  public  boolean   Oncreateoptionsmenu (Menu menu) {menu.add ( 0, //  menu group  1, //  menu item ID  0, //  menu item Sort Id "Option1"); //         menu item title  menu.add (0, 2, 1," Option2 " 0, 3, 2, "Option3" );  return  super  .oncreateoptionsmenu (menu); }

The first parameter that is required to add a menu item is the menu group ID (int type). The second parameter is the menu item ID, which is sent to the callback function when the menu item is selected. The third parameter represents a sort ID. The last parameter is the title of the menu item (that is, the name of the display).

In addition to the title parameter three parameters: Menu group, item ID, and menu item sort ID are optional, if you do not want to specify any ID, you can use Menu.none.

    • Working with menu groups
@Override      Public Boolean Oncreateoptionsmenu (Menu menu) {        int group1 = 1;         1, 1, "g1.item1");         2, 2, "g1.item2");         int group2 = 2;         3, 3, "g2.item1");         4, 4, "g2.item2");         return Super . Oncreateoptionsmenu (menu);    }

It is important to note that the menu item ID and sort ID are independent of the group.

Ways to manipulate menu groups:

    1. Removegroup (ID)--the parameter is the group ID, deleting all the Reorganized menu items
    2. Setgroupcheckable (id,checkable,exclusive)-Displays a tick mark in the menu when the menu item is selected. When applied to a group, it enables this feature for all menu items in that group. If the exclusive flag of this method is set to exclusive, only one of the menu items in the group is allowed to be checked. Other menu items remain in a checked state.
    3. Setgroupenabled (Id,boolean enabled)--Enables or disables menu items in a given group
    4. Setgroupvisible (id,visible)--Controls whether the reorganization menu item is visible
  • Response menu item
    1. Respond to menu items by onoptionsitemselected
      • @Override Public Booleanonoptionsitemselected (MenuItem item) {//TODO auto-generated Method Stub        Switch(Item.getitemid ()) { Case1: Toast.maketext (mainactivity. This, Item.gettitle (), 1000). Show ();  Break;  Case2: Toast.maketext (mainactivity. This, Item.gettitle (), 1000). Show ();  Break;  Case3: Toast.maketext (mainactivity. This, Item.gettitle (), 1000). Show ();  Break;  Case4: Toast.maketext (mainactivity. This, Item.gettitle (), 1000). Show ();  Break; }        return Super. onoptionsitemselected (item); }

        The key pattern here is to check the menu item ID through the MenuItem class's Getitemid () method, and then perform the necessary actions. If onoptionsitemselected () processes a menu item, it returns TRUE. This menu event will not propagate further. For onoptionsitemselected () unhandled menu item callbacks, onoptionsitemselected () should call the parent method through Super.onoptionsitemselected (). The default implementation of Onoptionsitemselected () returns false.

    2. Responding to menu items with listeners
      1. Implements the Onmenuclicklistener interface. It then obtains an instance of this implementation and passes it to the menu item. When you click a menu item, the menu item calls the Onmenuitemclick () method of the Onmenuclicklistener interface.
      2. classMyresponseImplementsonmenuitemclicklistener{@Override Public BooleanOnmenuitemclick (MenuItem item) {Switch(Item.getitemid ()) { Case1: Toast.maketext (mainactivity. This, Item.gettitle (), 1000). Show ();  Break;  Case2: Toast.maketext (mainactivity. This, Item.gettitle (), 1000). Show ();  Break;  Case3: Toast.maketext (mainactivity. This, Item.gettitle (), 1000). Show ();  Break;  Case4: Toast.maketext (mainactivity. This, Item.gettitle (), 1000). Show ();  Break; }            return true; }    }
        @Override Public BooleanOncreateoptionsmenu (Menu menu) {intgroup1 = 1; Menu.add (group1,1, 1, "G1.item1"); Menu.add (group1,2, 2, "g1.item2"); intGroup2 = 2; Menu.add (group2,3, 3, "G2.item1"); Menu.add (group2,4, 4, "g2.item2");  for(inti = 1; I < menu.size (); i++) {Menu.getitem (i). Setonmenuitemclicklistener (Newmyresponse ()); }        return Super. Oncreateoptionsmenu (menu); }
      3. The Onmenuitemclick method is called when a menu item is called. This code is executed when the menu item is clicked, even if the operation occurs before the onoptionsitemselected () method is called. If Onmenuitemclick returns True, no other callbacks, including the onoptionsitemselected () callback method, will be performed. This means that the listener code has a higher priority than the onoptionsitemselected () method.
    3. Using intent to respond to menu items
      1. Use the MenuItem Setintent (intent) method to associate a menu item with a intent. By default, the menu item has no intent associated with it. However, when intent is associated with a menu item and there is no other way to process the menu item, the default behavior is to call the intent using startactivity (intent). To do this, all handlers (especially the Onoptionitemselected method) should call the Onoptionitemselected () method of the parent class for the unhandled menu item. Or it can be understood that the system provides the Onoptionitemselected method with the opportunity to process the menu item first. This assumes that there is no listener directly associated with the menu item, and if so, the listener will overwrite the remaining menu items.
      2. If you do not override the Onoptionsitemselected () method, the base class in the Android framework will perform the necessary action to invoke Inten on the menu item. If you override this method but are not interested in this menu item, you must call the parent method and the parent method will then call intent. So the basic idea is this: either add the Onoptionsitemselected () method or rewrite it and call the parent method for the unhandled menu item.

       

      

  

The Android-menu menu uses a

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.