Basic menu knowledge in android, androidmenu

Source: Internet
Author: User

Basic menu knowledge in android, androidmenu

(1) option menu

1. simple menu creation:

1 @ Override 2 public boolean onCreateOptionsMenu (Menu menu) {3 super. onCreateOptionsMenu (menu); 4 5 // Add menu 6 menu. add ("menu option 1"); 7/* 8 * pram1: group number pram2: Unique ID number pram3: Sorting number pram4: Title 9 */10 menu. add (1, Menu. FIRST, Menu. FIRST, "menu option 2"); 11 12 // if you want to display the menu, return true13 return true; 14}

2. Menu grouping and simple attributes:

1 @ Override 2 public boolean onCreateOptionsMenu (Menu menu) {3 super. onCreateOptionsMenu (menu); 4 5 int group1 = 1; 6 int group2 = 2; 7 menu. add (group1, 1, 1, "item1"); 8 menu. add (group1, 2, 2, "item2"); 9 menu. add (group2, 3, 3, "item3"); 10 menu. add (group2, 4, 4, "item4"); 11 12 // menu. removeGroup (group1); // delete a group 13 // menu. setGroupVisible (group1, false); // sets the visibility 14 // menu. setGroupEnabled (group2, false); // you can set whether to click 15. // menu. setGroupCheckable (group2, true, t); // sets the check box for a group of menus. 16 return true; 17}

3. Response menu

1. onOptionsItemSelected

1 @ Override 2 public boolean onOptionsItemSelected (MenuItem item) {3 // responds to each menu item (by the menu item ID) 4 switch (item. getItemId () {5 case 1: 6 // do something here 7 break; 8 case 2: 9 // do something here10 break; 11 case 3: 12 // do something here13 break; 14 case 4: 15 Toast. makeText (this, "hello", 0 ). show (); 16 break; 17 default: 18 // For unhandled events, hand it to the parent class for 19 return super. onOptionsItemSelected (item); 20} 21 // return true indicates that the menu item event has been processed and does not need to be propagated until 22 return true; 23}

2. Listening

1 // Step 1: Create listener class 2 class MyMenuItemClickListener implements OnMenuItemClickListener {3 @ Override 4 publicboolean onMenuItemClick (MenuItem item) {5 // do something here... 6 return true; // finish handling 7} 8} 9 // Step 2: register the listener 10 menuItem for the menu item. setOnMenuItemClickListener (new MyMenuItemClickListener ());

3. Use Intent

 

(2) subMenu

Note: A Menu can contain multiple submenus and menuitems. A SubMenu can contain multiple menuitems, but a SubMenu cannot contain submenus.

1 @ Override 2 public boolean onCreateOptionsMenu (Menu menu) {3 // you can add multiple sub-menus to a menu. 4 SubMenu subMenu = menu. addSubMenu (, 0, "sub menu"); 5 6 // Add multiple menu options in the sub menu 7 MenuItem menuItem1 = subMenu. add (1, 1, 0, "menu option 1"); 8 subMenu. add (1, 2, 1, "menu option 2"); 9 subMenu. add (1, 3, 2, "menu option 3"); 10 subMenu. add (1, 4, 3, "menu option 4"); 11 12 // The Sub-menu does not support displaying icons, although this setting does not return 13 submenus. setIcon (R. drawable. ic_launcher); 14 15 // set the menu option icon 16 menuItem1.setIcon (R. drawable. ic_launcher); 17 return true; 18}

 

(3) Context Menu ContextMenu

1 package com. zzw. contextMenu; 2 3 import android. app. activity; 4 import android. OS. bundle; 5 import android. util. log; 6 import android. view. contextMenu; 7 import android. view. contextMenu. contextMenuInfo; 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 public class MainActivity ex Tends Activity {15 private static final String TAG = "MainActivity"; 16 ListView; 17 18 @ Override19 protected void onCreate (Bundle savedInstanceState) {20 Log. d (TAG, "onCreate"); 21 22 super. onCreate (savedInstanceState); 23 setContentView (R. layout. activity_main); 24 listView = (ListView) findViewById (R. id. listView); 25 26 simpleShowList (); 27 // 1. register the context menu for a view in the onCreate method of the activity 28 this. regist ErForContextMenu (listView); 29 30} 31 32 // 2. Generate the context menu in onCreateContextMenu. 33 @ Override34 public void onCreateContextMenu (ContextMenu menu, View v, 35 ContextMenuInfo menuInfo) {36 Log. d (TAG, "onCreateContextMenu_start"); 37 38 menu. setHeaderTitle ("context menu title"); 39 menu. add (0, 1, 0, "option 1"); 40 menu. add (0, 2, 0, "option 2"); 41 menu. add (0, 3, 0, "option 3"); 42 43 Log. d (TAG, "onCreateContextMenu_stop"); 44} 45 46 // 3. respond to context menu items in onContextItemSelected. 47 @ Override48 public boolean onContextItemSelected (MenuItem item) {49 Log. d (TAG, "onContextItemSelected_start"); 50 int item_id = item. getItemId (); 51 switch (item_id) {52 case :53 Toast. makeText (this, "I am context menu option 1", 0 ). show (); 54 break; 55 case 2: 56 57 break; 58 case 3: 59 60 break; 61 62 default: 63 return super. onContextItemSelected (item); 64} 65 Log. d (TAG, "onContextItemSelected_stop"); 66 return true; 67} 68 69 private void simpleShowList () {70 Log. d (TAG, "simpleShowList_start"); 71 String [] showList = {"show 1", "show 2", "show 3", "Show 4 "}; 72 73 ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, 74 android. r. layout. simple_expandable_list_item_1, showList); 75 76 listView. setAdapter (adapter); 77 Log. d (TAG, "simpleShowList_stop"); 78} 79 80}

 

 

(4) adding menus through XML files

1. Create a menu folder under the res File, which is usually existing by default. An Android xml File is generated to edit the menu.

2. Edit your own xml

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <menu xmlns: android = "http://schemas.android.com/apk/res/android"> 3 4 <! -- Group is a group --> 5 <group android: id = "@ + id/group1"> 6 7 <! -- Item is a menu option --> 8 <item 9 android: id = "@ + id/item1" 10 android: title = "menu option 1 (sub menu) "> 11 <! -- Add a menu to an item as the sub menu --> 12 <menu> 13 <! -- Menu options in the sub-menu --> 14 <item15 android: icon = "@ drawable/ic_launcher" 16 android: id = "@ + id/item1_1" 17 android: title = "option 1"/> 18 <item19 android: id = "@ + id/item1_2" 20 android: title = "option 2 in the sub-menu"/> 21 </menu> 22 </item> 23 <item24 android: id = "@ + id/item2" 25 android: title = "menu option 2"> 26 </item> 27 </group> 28 <group android: id = "@ + id/group2"> 29 <item30 android: id = "@ + id/item3" 31 android: title = "menu option 4"> 32 </item> 33 <item34 android: id = "@ + id/item4" 35 android: title = "menu option 5"> 36 </item> 37 </group> 38 39 </menu>

3. Get in activity:

 1 @Override 2     public boolean onCreateOptionsMenu(Menu menu) { 3         MenuInflater in = getMenuInflater(); 4         in.inflate(R.menu.main, menu); 5         return true; 6     } 7  8     @Override 9     public boolean onOptionsItemSelected(MenuItem item) {10         switch (item.getItemId()) {11         case R.id.item1:12            13             break;14         case R.id.item2:15 16             break;17         18         default:19             return super.onOptionsItemSelected(item);20         }21 22         return true;23     }

 

 

Some basic attributes:

Set menu icon

1 <item 2 android: id = "@ + id/item1" 3 android: title = "menu option 1" 4 android: icon = "@ drawable/ic_launcher" 5/>

Optional menu options: android: checkableBehavior and chencked

1 <! -- CheckableBehavior settings are optional --> 2 <group android: id = "@ + id/group1" 3 android: checkableBehavior = "all"> 4 <! -- Checked: set a specific menu item (checked) --> 5 <item 6 android: id = "@ + id/item1" 7 android: title = "menu option 1" 8> 9 </item> 10 <item11 android: id = "@ + id/item2" 12 android: checked = "true" 13 android: title = "menu option 2"> 14 </item> 15 </group>

 

Set Menu availability unavailable android: enable

1 <group android: id = "@ + id/group1"> 2 <item 3 android: id = "@ + id/item1" 4 android: title = "menu option 1" 5 android: enabled = "true" 6> 7 </item> 8 <item 9 android: id = "@ + id/item2" 10 android: title = "menu option 2" 11 android: enabled = "false"> 12 </item> 13 </group>

 

Set whether menu options are visible android: visible

1 <group android: id = "@ + id/group1"> 2 <item 3 android: id = "@ + id/item1" 4 android: title = "menu option 1" 5 android: visible = "true" 6> 7 </item> 8 <item 9 android: id = "@ + id/item2" 10 android: title = "menu option 2" 11 android: visible = "false"> 12 </item> 13 </group> 14

See: http://www.cnblogs.com/codingmyworld/archive/2011/08/21/2147829.html

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.