Android independent buttons and menu keys open menu

Source: Internet
Author: User

I won't talk much about it.

Go directlyCode

Package COM. example. menutest2; import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. menu; import android. view. menuitem; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class mainactivity extends activity {button; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); button = (button) findviewbyid (R. id. button2); button. setonclicklistener (New myonclicklistener ();} class myonclicklistener implements onclicklistener {public void onclick (view arg0) {// todo auto-generated method stubopenoptionsmenu ();}} @ overridepublic void openoptionsmenu () {// todo auto-generated method stubsuper. openoptionsmenu () ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// todo auto-generated method stubsuper. oncreateoptionsmenu (menu); int group1 = 1; int gourp2 = 2; menu. add (group1, 1, 1, "item 11"); menu. add (group1, 2, 2, "item 12"); menu. add (gourp2, 3, 3, "item 23"); menu. add (gourp2, 4, 4, "item 24"); Return true ;}@ overridepublic Boolean onoptionsitemselected (menuitem item) {// todo auto-generated method stubswitch (item. getitemid () {// respond to each menu item (by the menu item ID) Case 1: // do something herelog. I ("menutest:", "itemselected: 1"); break; Case 2: // do something herelog. I ("menutest:", "itemselected: 2"); break; Case 3: // do something herelog. I ("menutest:", "itemselected: 3"); break; Case 4: // do something herelog. I ("menutest:", "itemselected: 4"); break; default: // for events not processed, hand it to the parent class to process return Super. onoptionsitemselected (item);} // return true indicates that the menu item event is processed and does not need to be propagated. Return true ;} // @ override // public Boolean oncreateoptionsmenu (menu) {// getmenuinflater (). inflate (R. menu. activity_main, menu); // return true ;//}}

There is nothing to say about the above Code.

It is called when a key response is created:

 
Openoptionsmenu. Of course, if the menu still does not appear, you can refer to another blog:

Android does not display menu buttons.

Http://blog.csdn.net/penglijiang/article/details/7844514

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.