Android Create and respond to Options menu

Source: Internet
Author: User
Tags unique id

Create Options Menu

As mentioned earlier, the activity of Android 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 will only be executed when the option menu is displayed for the first time, and if you need to dynamically change the contents of the Options menu , use onPrepareOptionsMenu(Menu)

@Override
Publicboolean Oncreateoptionsmenu (Menu menu) {
Calling the parent class method to join the system menu
Although Android does not yet have a system menu, it is best to add it to a later version
Super.oncreateoptionsmenu (menu);

Add menu items (multiple ways)
1. Direct designation of headings
Menu.add ("menu item 1");
2. Assigning headings through resources
Menu.add (R.STRING.MENUITEM2);
3. Displays the group number, ID, sort number, title of the specified menu item
Menu.add (
1, //group number
Menu.first,//Unique ID number
Menu.first,//Sort number
"Menu item 3"); Title

Returns true if you want the menu to be displayed
Returntrue;
}

can also

上面的代码演示了添加菜单项的3种方法,下面解释下第三种方法Add (int groupId, int itemId, int order, charsequence title)。其中,第一个参数是组号,android中你可以给菜单分组,以便快速地操作同一组的菜单。第二个参数指定每个菜单项的唯一ID号,你可以自己指定,也可以让系统来自动分配,在响应菜单时你需要通过ID号来判断哪个菜单被点击了。因此常规的做法是定义一些ID常量,但在android中有更好的方法,就是通过资源文件来引用,这个之后介绍。第三个参数代表菜单项显示顺序的编号,编号小的显示在前面。

To group menu items
@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 as above, and after grouping, you can use the methods provided in menu to manipulate the groups, as follows:

Menu.removegroup (group1);    Delete a set of menus
Menu.setgroupvisible (gourp2, visible); Sets whether a set of menus is visible
Menu.setgroupenabled (GOURP2, enabled); Sets whether a set of menus can point
Menu.setgroupcheckable (Gourp2, checkable, exclusive); Set a check for a set of menus

Response menu item

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

1, through the Onoptionsitemselected method

The most commonly used method is to override the activity class's callback method, which is onOptionsItemSelected(MenuItem) called by Android whenever a menu item is clicked, and the clicked menu item is passed in.

@Override
Publicboolean onoptionsitemselected (MenuItem item) {
Switch (Item.getitemid ()) {
Respond to each menu item (by the ID of the menu item)
CASE1:
Do something here
Break
CASE2:
Do something here
Break
CASE3:
Do something here
Break
CASE4:
Do something here
Break
Default
To the parent class to handle events that are not handled
returnsuper.onoptionsitemselected (item);
}
Returns true to indicate that the event of the menu item has been processed, and that the event does not need to be propagated anymore.
Returntrue;
}

The above code can be used as a template to use the Onoptionsitemselected Method response menu , where the menu ID is hardcoded in the program for convenience, and you can use constants or resource IDs to make your code more robust.

2. Using listeners

Although the first method is recommended, Android also provides a way to respond to menus like the Java Swing listener. There are two steps to using the listener:

First step: Create a Listener class
Class Mymenuitemclicklistener implements Onmenuitemclicklistener {
@Override
Publicboolean Onmenuitemclick (MenuItem item) {
Do something here ...
Returntrue; Finish handling
}
}

Step Two: Register the Listener for the menu item
Menuitem.setonmenuitemclicklistener (New Mymenuitemclicklistener ());

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

3. Using the intent Response menu

The 3rd way is to raise the setintent (Intent Intent) method directly in MenuItem, so that Android will automatically be invoked when the menu is clicked startActivity(Intent) . But personally startActivity(Intent) , it's not as straightforward as calling it manually in Onoptionsitemselected's case.

Android Create and respond to options menu

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.