Android menu action creation and response menu

  • 2021-07-03 00:54:11
  • OfStack

The previous article "Learning to Understand Android Menu Menu Operation" briefly introduced the menu of Android under 1. Today, let's look at how to create and respond to the most commonly used option menu (options menu) through code under 1.

1. Create options menu

As mentioned earlier, activity of Android has already created android. view. Menu objects for us in advance, and provided the callback method onCreateOptionsMenu (Menu menu) for us to initialize the contents of the menu. This method will only be performed when the options menu is displayed for the first time. If you need to dynamically change the contents of the options menu, please use onPrepareOptionsMenu (Menu).


@Override
publicboolean onCreateOptionsMenu(Menu menu) {
 //  Call the parent class method to join the system menu 
 //  Although at present, android There is no system menu yet, but for compatibility with future versions, it is best to add 
super.onCreateOptionsMenu(menu);
 
 //  Add menu items (in many ways) 
 // 1. Specify the title directly 
 menu.add(" Menu item 1");
 // 2. Specify a title by resource 
 menu.add(R.string.menuitem2);
 // 3. Displays the group number of the specified menu item, ID , sort number, title 
 menu.add(
  1,  // Group number 
  Menu.FIRST, // Only 1 Adj. ID No. 
  Menu.FIRST, // Sequence number 
" Menu item 3"); // Title 
 
 //  If you want to display the menu, return true
returntrue;
}

The above code demonstrates three ways to add menu items, and the third method add (int groupId, int itemId, int order, CharSequence title) is explained below. Among them, the first parameter is the group number. In android, you can group menus to quickly operate menus in the same group. The second parameter specifies only 1ID number for each menu item. You can specify it yourself or let the system assign it automatically. When responding to the menu, you need to use ID number to determine which menu has been clicked. So the conventional approach is to define 1 ID constants, but a better approach in android is to refer to them through resource files, which will be described later. The third parameter represents the number of menu items displayed in the order, and the smaller number is displayed in front.

2. Grouping menu items


@Override
publicboolean onCreateOptionsMenu(Menu menu) {
 super.onCreateOptionsMenu(menu);
 //  Add 4 Menu items, divided into 2 Group 
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 menu items as above, and after grouping, you can use the methods provided in menu to operate on the group, as follows:


menu.removeGroup(group1); // Delete 1 Group menu 
menu.setGroupVisible(gourp2, visible); // Settings 1 Is the group menu visible 
menu.setGroupEnabled(gourp2, enabled); // Settings 1 Is the group menu clickable 
menu.setGroupCheckable(gourp2, checkable, exclusive); // Settings 1 Check status of group menu 

3. Respond to menu items

android provides a variety of ways to respond to menu items, described in 11 below

1. Through onOptionsItemSelected method
The most commonly used method is to override the onOptionsItemSelected (MenuItem) callback method of the activity class, which android calls whenever a menu item is clicked and passes in the clicked menu item.


@Override
publicboolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
 // Respond to each menu item ( Through the menu item's ID)
case1:
 // do something here
break;
 case2:
 // do something here
break;
 case3:
 // do something here
break;
 case4:
 // do something here
break;
 default:
 // For unhandled events, hand them over to the parent class for processing 
returnsuper.onOptionsItemSelected(item);
 }
 // Return true Indicates that the event of the menu item has been processed, so there is no need to continue propagating the event 
returntrue;
}

The above code can be used as a template for responding to menus using the onOptionsItemSelected method. Here, for convenience, the menu ID is hard-coded in the program. You can use constants or resources ID to make the code more robust.

Step 2 Use a listener
Although the first method is recommended, android provides a listener mode similar to java swing to respond to menus. There are two steps to using listeners:


// No. 1 1 Step: Create a listener class 
class MyMenuItemClickListener implements OnMenuItemClickListener {
 @Override
 publicboolean onMenuItemClick(MenuItem item) {
 // do something here...
returntrue; //finish handling
 }
}

// No. 1 2 Step: Register listeners for menu items 
menuItem.setOnMenuItemClickListener(new MyMenuItemClickListener());

The description of onMenuItemClick (MenuItem item) callback method in android document is "Called when a menu item has been invoked. This is the first code that is executed; if it returns true, no other callbacks will be executed.

3. Use the Intent Response Menu
The third way is to call the setIntent (Intent intent) method directly on MenuItem, so that android automatically calls startActivity (Intent) when the menu is clicked. However, I think it is better to call startActivity (Intent) directly in case of onOptionsItemSelected.

Conclusion

This article introduces how to create and respond to option menus in detail. The next article "SubMenu and IconMenu of Android menu operation" will continue to introduce the use of submenus and icon menus.


Related articles: