Hello everyone, today we are talking about the design of the android function menu. Two menu items are defined in the program. One is "about" and the other is "quit ", when you click "about", createToast
Prompt: When you click "exit", we will end the program.
In addition toOncreate
In addition, you also need to create two other class functions:Oncreateoptionsmenu
() AndOnoptionsitemselected
(), The former is the creationMenu
The 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
The Code is as 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;
}
}