[Android self-study Note 2] adds a menu for your android Program

Source: Internet
Author: User

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

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.