Android programming-option menu & context menu basic usage

Source: Internet
Author: User

I found that I spent a lot of time learning every android interface control. Because I am a beginner, I may still need to improve my learning methods and methods.

This document describes the basic implementation and usage of option menus and context menus.

The option menu is the bottom menu popped up by pressing the menu key of the mobile phone. A maximum of six items are displayed at the same time. More items are displayed as more... click to submenu.

// Add a menu item

@ Overridepublic Boolean oncreateoptionsmenu (menu) {// todo auto-generated method stubmainmenu = menu; mainmenu. add (0, 1, menu. none, "menu 1 "). seticon (R. drawable. icon); mainmenu. add (0, 2, menu. none, "menu 2"); mainmenu. add (0, 3, menu. none, "menu 3"); mainmenu. add (1, 4, menu. none, "menu 4"); mainmenu. add (1, 5, menu. none, "menu 5"); mainmenu. add (1, 6, menu. none, "menu 6"); // The first parameter of ADD is the group number. You can click the button to hide the return Super. oncreateoptionsmenu (menu );}

// Implement menu Behavior

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// TODO Auto-generated method stubsetTitle(item.getItemId() + item.getTitle().toString());return super.onOptionsItemSelected(item);}

// Hide a group menu function and write it in the listener function of a button

Private onclicklistener toggle_listen = new onclicklistener () {public void onclick (view v) {If (misgroupvisible = true) {mainmenu. setgroupvisible (1, false); // hide the menu group misgroupvisible = false;} else {mainmenu. setgroupvisible (1, true); misgroupvisible = true ;}}};

// Register the oncreat function to accept the long-pressed response context menu

// Register the context menu this. registerforcontextmenu (findviewbyid (R. Id. longan2) for two text boxes; this. registerforcontextmenu (findviewbyid (R. Id. Longan ));

// Add a long-pressed menu item

@ Overridepublic void oncreatecontextmenu (contextmenu menu, view V, contextmenuinfo menuinfo) {// todo auto-generated method stubswitch (v. GETID () {case R. id. longan: // If (menu. hasvisibleitems () return; menu. add (2, 11, menu. none, "menu 1"); submenu master = menu. addsubmenu (4, 41, 1, "Master .. "); // submenu is a sub-menu with only one layer. You can click master. add ("WT1"); master. add ("WT2"); master. add ("wt3"); menu. add (2, 12, menu. none, "menu 2"); menu. add (2, 13, menu. none, "menu 3"); menu. add (2, 14, menu. none, "menu 4"); break; case R. id. longan2: menu. add (3, 21, menu. none, "menu 21"); menu. add (3, 22, menu. none, "menu 22"); break;} super. oncreatecontextmenu (menu, V, menuinfo );}

// Menu response function, similar to the previous option menu

@Overridepublic boolean onContextItemSelected(MenuItem item) {// TODO Auto-generated method stubsetTitle(item.getTitle());return super.onContextItemSelected(item);}


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.