Android Menu Action creation and Response menu _android

Source: Internet
Author: User
Tags unique id

The previous article on "Learning to understand Android menu menus" briefly introduces Android's menu, and today let's take a look at how to create and respond to the most commonly used option menus (Options menu) through code.

First, create Options menu

As mentioned earlier, the Android activity has created a Android.view.Menu object for us in advance and provided a callback method Oncreateoptionsmenu (Menu menu) for us to initialize the contents of the menus. This method is only executed when the option menu is first displayed, and if you need to dynamically change the contents of the Options menu, use Onprepareoptionsmenu (menu).

@Override
publicboolean oncreateoptionsmenu (Menu menu) {
 //Invoke the parent class method to join the System menu
 //Although Android does not yet have a system menu, But in order to be compatible with later versions, it is best to add
super.oncreateoptionsmenu (menu);
 
 Add menu items (various ways)
 //1. Directly specify
 the title menu.add ("menu item 1");
 2. Designation of title
 Menu.add (R.STRING.MENUITEM2) through resources
 ; 3. Display the group number, ID, sort number, title
 menu.add (
  1,//  group number Menu.first,//
  unique ID number
  Menu.first,//sorting number
for the specified menu item. "Menu item 3"); Title
 
 //If you want to display the menu, return True
returntrue;
}

The above code illustrates 3 ways to add a menu item, and the third method, add (int groupId, int itemId, int order, charsequence title), is explained below. Where the first parameter is the group number, you can group the menus in Android so that you quickly manipulate the same set of menus. The second parameter specifies the unique ID number of each menu item, you can specify it yourself, or you can have the system assign it automatically, and in response to the menu you need to use the ID number to determine which menu is clicked. So the general practice is to define some ID constants, but there's a better way to do it in Android, which is to refer to the resource file, which is described later. The third parameter represents the number of the order in which the menu item is displayed, and the small number appears in front.

Second, to the menu item grouping

@Override
publicboolean oncreateoptionsmenu (Menu menu) {
 Super.oncreateoptionsmenu (menu);
 Add 4 menu items, divided into 2 groups
int group1 =1;
 int GOURP2 =2;
 Menu.add (group1, 1, 1, "Item 1");
 Menu.add (Group1, 2, 2, "Item 2");
 Menu.add (GOURP2, 3, 3, "Item 3");
 Menu.add (GOURP2, 4, 4, "Item 4");
 Show menu
returntrue;
}

You can group the menu items like this, and after grouping, you can use the methods provided in the menu to manipulate the group, as follows:

Menu.removegroup (group1); Delete a set
of menus menu.setgroupvisible (GOURP2, visible);//Set whether a set of menus is visible
menu.setgroupenabled (GOURP2, enabled); Set a set of menus to point to
menu.setgroupcheckable (GOURP2, checkable, exclusive);//Set up a list of menu options

Iii. Response Menu Items

Android offers a variety of ways to respond to menu items, which are described below

1, through the Onoptionsitemselected method
The most common method used is to rewrite the onoptionsitemselected (MenuItem) callback method for the activity class, which is invoked by Android whenever a menu item is clicked and passed in to the clicked menu item.

@Override
publicboolean onoptionsitemselected (MenuItem item) {
 switch (Item.getitemid ()) {
 //response to each menu item ( Through the ID of the menu item
case1:
 //Do something-here-break
;
 CASE2:
 //do something here
;
 CASE3:
 //do something here
;
 CASE4:
 //do something here
;
 Default:
 //For unhandled events, give the parent class the
returnsuper.onoptionsitemselected (item);
 }
 Returns true to the event that the menu item was processed without the need to continue to propagate the event
Returntrue;

The above code can be used as a template for using the Onoptionsitemselected Method Response menu, where you can use constants or resource IDs to make your code more robust by hard-coded the menu ID in your program for convenience.

2. Using the Listener
While the first approach is the recommended approach, Android provides a way to respond to menus like a Java swing listener. The way to use listeners is two steps:

Step one: Create Listener Classes Class
Mymenuitemclicklistener implements Onmenuitemclicklistener {
 @Override
 Publicboolean Onmenuitemclick (MenuItem Item) {
 //do something
... Returntrue; Finish handling
 }

//Step two: Register listener menuitem.setonmenuitemclicklistener for menu items
(new Mymenuitemclicklistener ());

The Android document's description of the Onmenuitemclick (MenuItem Item) callback method is "Called when a menu item has been invoked." This is the executed; If it returns true, no other callbacks would be executed. Visible this method is performed before onoptionsitemselected.

3. Using the intent Response menu
The 3rd way is to raise the Setintent (Intent Intent) method directly on MenuItem so that Android automatically calls StartActivity (Intent) when the menu is clicked. But I think it's better to call StartActivity (Intent) directly in Onoptionsitemselected's case.

Conclusion

This article details how to create and respond to Options menu, the next "Android Menu Operation submenu and Iconmenu" will continue to introduce the submenu and icon menu use, we continue to focus on OH.

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.