Options menu in Android (optionmenu) use case

Source: Internet
Author: User

Each activity in Android is bundled with a optionmenu, which is initialized by invoking the Oncreateoptionmenu (Menu menu) function, which we can override to set a response menu for the activity. Here's a small example of how this is done.

 PackageCom.yangzi.optionmenu;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;/* * After creating an Android project, go to the Mainactivity class, overwrite the Oncreateoptionsmenu method and onoptionitemselected method here * */ Public  class mainactivity extends Activity {    //Declaration menu sub-option ID number    Private Static Final intITEM1 = Menu.first;Private Static Final intITEM2 = item1+1;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);    Setcontentview (R.layout.activity_main); }/ * Override this method to generate a menu * * /    @Override     Public Boolean Oncreateoptionsmenu(Menu menu) {the//add method is to add a submenu of the submenu        /* For parameter description of the Add method: * Parameter one: Group ID (int): Set ID to indicate which group the sub-option belongs to * parameter two: item ID (int): Sub-option ID, which is used to indicate the sub-option          * Parameter three: order ID (int): Display order, indicating the display order of the sub-option, default to 0, indicating in addition order * parameter four: title (String): Used to define the display text of the sub-option * * */Menu.add (0, ITEM1,0,"Start"); Menu.add (0, ITEM2,0,"Exit");return true; }/ * Overwrite the method and add the corresponding menu event * */    @Override     Public Boolean onoptionsitemselected(MenuItem Item) {/* * Depending on the selected sub-option (differentiated by item ID), different responses are made * * */        Switch(Item.getitemid ()) { CaseItem1:settitle ("Start the game");//The title of the activity is set here             Break; CaseItem2:settitle ("Exit"); Break; }return true; }}

If you want to see the effect, try it yourself (-_-)

Options menu in Android (optionmenu) use case

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.