Hello everyone, today we are talking about the design of the android function menu, Program Two menu items are defined, one is "about" and the other is "exit". When you click "about", a new one is created.
Toast Prompt: When you click "exit", we will end the program.
In addition toOncreateIn addition, you also need to create two other class functions:Oncreateoptionsmenu() AndOnoptionsitemselected(), The former is the creationMenuThe menu item, which is used to process the event after the menu is selected for running.
Take a look:
We only made changes in one file, that isMenudemo. Java,CodeAs follows:
Package com. Android. test;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. widget. Toast;
Public class menudemo extends activity {
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
// Create a menu
Public Boolean oncreateoptionsmenu (menu ){
Menu. Add (0, 0, 0, "about ");
Menu. Add (0, 1, 1, "quit ");
Return super. oncreateoptionsmenu (menu );
}
// Menu response
Public Boolean onoptionsitemselected (menuitem item ){
Super. onoptionsitemselected (item );
Switch (item. getitemid ()){
Case 0:
Toast. maketext (menudemo. This, "Welcome to Wei zhulin's blog", Toast. length_long). Show ();
Case 1:
This. Finish ();
}
Return true;
}
}