Android Development Review notes-Menu menu component (option Menu, context menu, sub Menu), Android menu

Source: Internet
Author: User

Android Development Review notes-Menu menu component (option Menu, context menu, sub Menu), Android menu

Menus are one of the most common elements in the user interface and are frequently used. In Android, menus are divided into three types: OptionsMenu and ContextMenu) and SubMenu ).

There are two menu implementation methods: one is to generate a menu by layout file xml, and the other is to generate a menu by code.

The three menus have a lot of content, but they are similar in general. I prefer to dynamically generate the code. The following uses the Code as an example.

 

1. option menu (OptionsMenu)

First, let's look at the options menu:

 

Click the Menu key of the mobile phone in an Activity interface. The Menu displayed below the screen is called the OptionsMenu ).

 

Step 1: override the onCreateOptionsMenu (Menu menu) function of Activity 2. Call the add () method of menu to add a Menu item (MenuItem ), you can call the setIcon () method of MenuItem to set the icon for the menu. 3. When the menu item () is selected, we can respond to the event by overwriting the onOptionsItemSeleted () method of the Activity.

First, let's take a look at the main methods:

1 public boolean onCreateOptionsMenu (Menu menu): // use this method to call OptionsMenu. 2 3 public boolean onOptionsItemSelected (MenuItem item): // The action that occurs after the menu item is selected. 4 5 public void onOptionsMenuClosed (Menu menu): // The action that occurs after the Menu is closed. 6 7 public boolean onPrepareOptionsMenu (Menu menu): // The onPrepareOptionsMenu method is called before the option Menu is displayed. You can use this method to adjust the menu according to the current situation. 8 9 public boolean onMenuOpened (int featureId, Menu menu): // action that occurs after a single open.

Next, let's look at a small Demo, which provides a wide range of comments.

1 package com. example. menutest; 2 3 import android. app. activity; 4 import android. content. intent; 5 import android. OS. bundle; 6 import android. view. menu; 7 import android. view. menuItem; 8 import android. widget. toast; 9 10/** 11*12 * @ author Balla _ rabbit 13 * use code to dynamically generate a menu (option menu) 14*15 */16 public class MainActivity extends Activity {17 18 @ Override19 protected void onCreate (Bundle savedInstanceState) {20 super. onCreate (savedInstanceState); 21 setContentView (R. layout. activity_main); 22} 23 24 @ Override25 // onCreateOptionsMenu () This method is called only once, that is, it is called when it is displayed for the first time. 26 public boolean onCreateOptionsMenu (Menu menu) {27 // MenuItem item1 = menu. add (1, 1, 1, "first option menu"); 28 // item1.setTitle ("first option menu"); 29 // item1.setIcon (R. drawable. ic_launcher); 30 menu. add (1, 1, 1, "first option menu"); 31 menu. add (1, 2, 1, "second option menu"); 32 menu. add (1, 3, 1, "third option menu"); 33 menu. add (1, 4, 1, "fourth option menu"); 34 return super. onCreateOptionsMenu (menu); 35} 36 37 @ Override38 // if you need to update the menu item. you can operate on the onPrepareOptionsMenu () method. 39 public boolean onPrepareOptionsMenu (Menu menu) {40 Toast. makeText (this, "method executed before menu display", Toast. LENGTH_SHORT ). show (); 41 return super. onPrepareOptionsMenu (menu); 42} 43 44 @ Override45 public void onOptionsMenuClosed (Menu menu) {46 Toast. makeText (this, "method executed after the menu is closed", Toast. LENGTH_SHORT ). show (); 47 super. onOptionsMenuClosed (menu); 48} 49 50 @ Override51 // when the menu is selected, it is implemented in the OnOptionsItemSelected () method. 52 public boolean onOptionsItemSelected (MenuItem item) {53 switch (item. getItemId () {// obtain the menu id54 case // you can use Intent to jump to the interface 56 Intent intent = new Intent (MainActivity. this, HelloWorld. class); 57 item. setIntent (intent); 58 break; 59 case 2: 60 Toast. makeText (this, "the second option menu is clicked", Toast. LENGTH_SHORT ). show (); 61 break; 62 case 3: 63 Toast. makeText (this, "the third option menu is clicked", Toast. LENGTH_SHORT ). show (); 64 break; 65 case 4: 66 Toast. makeText (this, "the fourth option menu is clicked", Toast. LENGTH_SHORT ). show (); 67 break; 68} 69 return super. onOptionsItemSelected (item); 70} 71 72}

 

In the above example, there is an add method in the create menu method onCreateOptionsMenu (). Its return value is a MenuItem. We can set more options based on its return value, such as the menu icon.

Let's take a look at its parameters. below is the explanation in its corresponding API documentation: parameter 1: grouping, parameter 2: identifier id, parameter 3: sorting, parameter 4: menu text

 

2. Context Menu

International Practices:

To create a context menu:

1. Override the onCreateContenxtMenu () method of the Activity and call the add method of Menu to add a Menu item (MenuItem ).

2. overwrite the onContextItemSelected () method of the Activity to respond to the Click Event of the context menu item.

3. Call the registerForContextMenu () method to register the context menu for the view.

 

The following is an example of the previous Demo. The ListView component will not be annotated here. If you are not clear about it, you can refer to my previous articles.

1 package com. example. menutest; 2 3 import android. app. activity; 4 import android. OS. bundle; 5 import android. view. contextMenu; 6 import android. view. contextMenu. contextMenuInfo; 7 import android. view. menu; 8 import android. view. menuItem; 9 import android. view. view; 10 import android. widget. arrayAdapter; 11 import android. widget. listView; 12 import android. widget. toast; 13 14/** 15*16 * @ author Balla _ rabbit 17 * use code to dynamically generate a menu (context menu) 18*19 */20 public class MainActivity extends Activity {21 22 private ListView listview; 23 private ArrayAdapter <String> adapter; 24 private String info [] = {"option 1 ", "option 2", "option 3", "option 4", "option 5", "Option 6"}; 25 26 @ Override27 protected void onCreate (Bundle savedInstanceState) {28 super. onCreate (savedInstanceState); 29 setContentView (R. layout. activity_main); 30 // create a ListView list 31 listview = (ListView) findViewById (R. id. listview); 32 adapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, info); 33 listview. setAdapter (adapter); 34 35 36 // register the context menu for the current View. 37 this. registerForContextMenu (listview); 38 39} 40 41 @ Override42 public void onCreateContextMenu (ContextMenu menu, View v, 43 ContextMenuInfo menuInfo) {44 menu. setHeaderTitle ("I Am a context menu"); // set context menu question 45 menu. setHeaderIcon (R. drawable. ic_launcher); // sets the context menu question icon 46 menu. add (1, 1, 1, "I am context menu option 1"); 47 menu. add (1, 2, 1, "I am context menu option 2"); 48 menu. add (1, 3, 1, "I am context menu option 3"); 49 menu. add (1, 4, 1, "I'm context menu option 4"); 50} 51 52 @ Override53 public boolean onContextItemSelected (MenuItem item) {54 switch (item. getItemId () {55 case 1: 56 Toast. makeText (this, "click context menu option 1", Toast. LENGTH_SHORT ). show (); 57 break; 58 case 2: 59 Toast. makeText (this, "click context menu option 2", Toast. LENGTH_SHORT ). show (); 60 break; 61 case 3: 62 Toast. makeText (this, "click context menu option 3", Toast. LENGTH_SHORT ). show (); 63 break; 64 case 4: 65 Toast. makeText (this, "click context menu option 4", Toast. LENGTH_SHORT ). show (); 66 break; 67 68} 69 return super. onContextItemSelected (item); 70} 71 72 @ Override73 // disable context Menu to trigger this method 74 public void onContextMenuClosed (menu Menu) {75 Toast. makeText (this, "Disable context menu", Toast. LENGTH_SHORT ). show (); 76} 77 78 79 80 81 82}

 

3. Sub-menu

Let's take a look at it first:

 

 

To create a sub-menu:
1. Override the onCreateOptionsMenu () method of the Activity and call the addSubMenu () method of the Menu to add sub-menus.
2. Call the add () method of SubMenu to add a sub menu item (addSubMenu)
3. Override the onOptionsItemSelected () method to respond to the Click Event of the sub-menu.

Sub-menus provide a natural way to organize menu items. We can use addSubMenu (int groupId, int itemId, int order, int titleRes) this method is very convenient for creating and responding to sub-menus.

 

The code is similar to the option menu created earlier, and the context menu is similar. The code is displayed directly:

1 package com. example. menutest; 2 3 import android. app. activity; 4 import android. OS. bundle; 5 import android. view. menu; 6 import android. view. menuItem; 7 import android. view. subMenu; 8 import android. widget. toast; 9 10/** 11*12 * @ author Balla _ rabbit 13 * use code to dynamically generate a menu (sub menu) 14*15 */16 public class MainActivity extends Activity {17 18 19 @ Override20 protected void onCreate (Bundle savedInstanceState) {21 super. onCreate (savedInstanceState); 22 setContentView (R. layout. activity_main); 23} 24 25 @ Override26 public boolean onCreateOptionsMenu (Menu menu) {27 SubMenu s1 = menu. addSubMenu (1, 0, 1, "I am menu 1"); 28 s1.setIcon (R. drawable. ic_launcher); // sets the sub-menu icon 29 s1.setHeaderTitle ("sub-menu"); // sets the sub-menu title 30 s1.add (1, 1, 1, "I am submenu 1"); 31 s1.add (1, 2, 1, "I am submenu 2"); 32 s1.add (1, 3, 1, "I am SubMenu 3"); 33 s1.add (1, 4, 1, "I am SubMenu 4"); 34 35 SubMenu s2 = menu. addSubMenu (2, 0, 1, "I am menu 2"); 36 s2.setIcon (R. drawable. ic_launcher); 37 s2.setHeaderTitle ("sub menu"); 38 s2.add (2, 1, 1, "I am sub menu (1)"); 39 s2.add (2, 2, 1, "I am a sub menu (2)"); 40 s2.add (2, 3, 1, "I am a sub menu (3)"); 41 s2.add (2, 4, 1, "I am submenu (4)"); 42 43 return super. onCreateOptionsMenu (menu); 44} 45 46 @ Override47 public boolean onOptionsItemSelected (MenuItem item) {48 if (item. getGroupId () = 1) {// judge group 49 switch (item. getItemId () {50 case 1: 51 Toast. makeText (MainActivity. this, "submenu 1 of menu 1 is clicked", Toast. LENGTH_SHORT ). show (); 52 break; 53 case 2: 54 Toast. makeText (MainActivity. this, "submenu 2 of menu 1 is clicked", Toast. LENGTH_SHORT ). show (); 55 break; 56 case :57 Toast. makeText (MainActivity. this, "submenu 3 of menu 1 is clicked", Toast. LENGTH_SHORT ). show (); 58 break; 59 case 4: 60 Toast. makeText (MainActivity. this, "sub-menu 4 of menu 1 is clicked", Toast. LENGTH_SHORT ). show (); 61 break; 62 63} 64} else if (item. getGroupId () = 2) {65 switch (item. getItemId () {66 case 1: 67 Toast. makeText (MainActivity. this, "submenu 1 of menu 2 is clicked", Toast. LENGTH_SHORT ). show (); 68 break; 69 case 2: 70 Toast. makeText (MainActivity. this, "sub-Menu 2 of menu 2 is clicked", Toast. LENGTH_SHORT ). show (); 71 break; 72 case 3: 73 Toast. makeText (MainActivity. this, "submenu 3 of menu 2 is clicked", Toast. LENGTH_SHORT ). show (); 74 break; 75 case 4: 76 Toast. makeText (MainActivity. this, "sub-menu 4 of menu 2 is clicked", Toast. LENGTH_SHORT ). show (); 77 break; 78} 79} 80 81 return super. onOptionsItemSelected (item); 82} 83 84}

 


For Android development and java, which of the following methods is onCreateOptionsMenu?

Android has three menus:
1. option menu (optinosMenu)
2. Context menu (ContextMenu)

3. subMenu)
OptionsMenu is the most commonly used option menu, which is displayed at the bottom of the corresponding Activity after you click the menu button. If you have used an android phone, it should be obvious.

On Android phones, how does one make the sub-menu under the setup program a desktop shortcut?

The principle of adding an activity link in the settings is to add the activity link to the desktop. during use, it is found that some desktops (such as LP sense UI) integrate this function. The usage is as follows: after the desktop is installed, long press the desktop, select "add", find the Activity Options, click it, and display the activity of all the software installed on the mobile phone. Find the settings in the drop-down list, after expansion, you can select the shortcut of the required sub-menu as needed. Note that only some settings can be entered through the shortcut.
 

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.