In Android applications, menus provide users with software usage methods and some information about software producers. Similarly, many exit menus of software are also in menus. Here I will briefly introduce how to add the menu:
1. The menu is added to the activity. To add a menu, you need to override two methods: oncreateoptionsmenu (menu) and onoptionsitemselected (menuitem item)
2. The first method oncreateoptionsmenu (menu) is used to add the display items of the menu. The add method has four different methods.
Add (charsequence title)
Add (INT groupid, int
Itemid, int order, int titleres)
Add (INT titleres)
Add (int
Groupid, int Itemid, int order, charsequence title)
You can select different parameters as needed.
3. The second method onoptionsitemselected (menuitem item) is used to specify the event triggered after the corresponding item is selected.
The following uses an example to describe how to use it.
// Implement the menu function @ overridepublic Boolean onoptionsitemselected (menuitem item) {// todo auto-generated method stubsuper. onoptionsitemselected (item); Switch (item. getitemid () {Case 0: New alertdialog. builder (this ). settitle ("usage "). setmessage ("face changing functions can be realized if you shake a mobile phone or use your hand to flat the screen of a mobile phone (without touching the screen of a mobile phone "). setpositivebutton ("OK", // here is the event new dialoginterface for adding and clicking OK. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int whi Ch) {// todo auto-generated method stub }}). show (); break; Case 1: New alertdialog. builder (this ). settitle ("about this software "). setmessage ("by dreamtale "). setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub }}). show (); break; Case 2: New alertdialog. builder (this ). settitle ("quit "). setmessage ("are you sure you want to exit? "). Setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stubfinish ();}}). setnegativebutton ("cancel", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub }}). show (); break;} return true ;}
As follows:
This is the effect of clicking menu.
Ask when you exit