Analysis of three menu examples of Android

  • 2020-06-19 11:41:54
  • OfStack

This article illustrates the three menus of Android. Share to everybody for everybody reference. The specific analysis is as follows:

There are three types of Android menus: option menu (Option Menu), context menu (Context Menu), and submenu (Sub Menu)

1. In the options menu

When the user clicks the menu button on the device (Menu), the menu that triggers the event is the options menu. The options menu has a maximum of six options, beyond which the sixth will automatically display more options to display the display.
Creation method:

1. Override onCreateOptionsMenu(Menu menu) method, which is called when we open the menu for the first time.
2. Call add() method of Menu to add menu item (MenuItem). You can call setIcon() method of MenuItem to set ICONS for menu item.
3. When the menu item (MenuItem) is selected, the onOptionsMenuSelected() method that overrides Acitivy responds to the event.

2. Context menu

When the user long presses the Activity page, the pop-up menu is called the context menu. We often right-click on Windows and the menu that pops up is the context menu.

1. Override onCreateContextMenu() method of Activity and call add method of Menu to add menu item MenuItem
2. Override onContextItemSelected() method and respond to menu click event
3. Call registerForContextMenu() to register the context menu for the view

3. The sub menu

Submenu is a kind of menu that groups the same function for multi-level display. For example, in the "File" menu of Windows, there are "New", "Open", "close" and other submenus.

Method to create a submenu

1. Override the onCreateOptionsMenu() method of Activity and call the addSubMenu() method of Menu to add submenu items
2. Call add() mealtimes of SubMenu and add submenu items
3. Override onCreateItemSelected() method and respond to menu click event


public class Main extends Activity { 
//  A menu item ID 
// FIRST for Menu Class  
private static final int ITEM1 = Menu.FIRST; 
private static final int ITEM2 = Menu.FIRST + 1; 
private static final int ITEM3 = Menu.FIRST + 2; 
private static final int ITEM4 = Menu.FIRST + 3; 
private static final int ITEM5 = Menu.FIRST + 4; 
private static final int ITEM6 = Menu.FIRST + 5; 
private static final int ITEM7 = Menu.FIRST + 6; 
TextView myTV; 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    myTV = (TextView) findViewById(R.id.myTV); 
    //  At this time for myTv To set the context menu, press long TextView Time response function  
    registerForContextMenu(myTV); 
  } 
@Override 
//  Click the menu button in response to the event  
public boolean onCreateOptionsMenu(Menu menu) { 
 /* 1 The following code is an option menu test 
   //  Add menu item 
 // public abstract MenuItemadd(int groupId, int itemId, int order, CharSequence title)
 //  Where the menu is group , the menu of ID , sequence, display text 
 // add() Method returns 1 a MenuItem Object. while setIcon is MenuItem The method of 
 menu.add(0, ITEM1, 0, " start ").setIcon(R.drawable.ic_launcher);
 menu.add(0, ITEM2, 0, " start 1");
 menu.add(1, ITEM3, 0, " start 2");
 menu.add(1, ITEM4, 0, " start 3");
 menu.add(1, ITEM5, 0, " start 4");
// menu.add(0, ITEM6, 0, " start 5");
// menu.add(0, ITEM7, 0, " start 6");
 */ 
 /**
  *  The following code is the test code to add a submenu 
  */ 
 //  Add submenus  
 SubMenu subFile = menu.addSubMenu(" file "); 
 SubMenu editFile = menu.addSubMenu(" The editor "); 
 //  Adds a menu item to a submenu  
 subFile.add(0, ITEM1, 0, " new "); 
 subFile.add(0, ITEM2, 0, " Open the "); 
 return true; 
} 
@Override 
//  The event that is triggered when the menu is selected  
public boolean onOptionsItemSelected(MenuItem item) { 
 /*
 //  In this show 1 Next, Menu The equivalent of 1 A container, and MenuItem The equivalent of what's in the container 
 switch(item.getItemId()) {
 case ITEM1:
  //  Set up the Activity the Title
  setTitle(" Start the game 1");
  break;
 case ITEM2:
  setTitle(" Start the game 2");
  break;
 case ITEM3:
  setTitle(" Start the game 3");
  break;
 case ITEM4:
  setTitle(" Start the game 4");
  break;
 case ITEM5:
  setTitle(" Start the game 5");
  break;
 case ITEM6:
  setTitle(" Start the game 6");
  break;
 case ITEM7:
  setTitle(" Start the game 7");
  break;
 }
 */ 
 /**
  *  Submenu item response code 
  */ 
 switch(item.getItemId()) { 
 case ITEM1: 
  //  Set up the Activity the Title 
  setTitle(" The new file "); 
  break; 
 case ITEM2: 
  setTitle(" Open the file "); 
  break; 
 } 
 return true; 
} 
@Override 
//  Create context menu  
public void onCreateContextMenu(ContextMenu menu, View v, 
  ContextMenuInfo menuInfo) { 
 //  Add a menu item to the following menu  
 //  Notice that the menu is ContextMenu 
 menu.add(0, ITEM1, 0, " Red background "); 
 menu.add(0, ITEM2, 0, " Green background "); 
 menu.add(1, ITEM3, 0, " A white background "); 
} 
@Override 
public boolean onContextItemSelected(MenuItem item) { 
 switch(item.getItemId()) { 
 case ITEM1: 
  myTV.setBackgroundColor(Color.RED); 
  break; 
 case ITEM2: 
  myTV.setBackgroundColor(Color.GREEN); 
  break; 
 case ITEM3: 
  myTV.setBackgroundColor(Color.WHITE); 
  break; 
 } 
 return true; 
}
}

I hope this article has been helpful for your Android programming.


Related articles: