Android Learning (23) Submenu submenu

Source: Internet
Author: User

One, submenu sub-menu

Similar to the feature menu, but you can add submenus.

Second, the implementation of the steps:

1. Create submenus through the Oncreateoptionsmenu method, which can be created dynamically through code or through XML.

2, through the Onoptionsitemselected method to achieve click on the sub-menu events.

Third, the reference code:

1. Create a submenu dynamically from the code:

Main.java

 PackageCom.example.zhengcheng.submenudemo;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.SubMenu;ImportAndroid.widget.Toast; Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//dynamically creating submenus from codesubmenu Menu_file = Menu.addsubmenu ("file");//Create File menusubmenu Menu_edit = Menu.addsubmenu ("edit");//Create an Edit menu//Create a submenu for the File menuMenu_file.add (1, 101, 1, "new"); Menu_file.add (1, 102, 1, "open"); Menu_file.add (1, 103, 1, "Save"); Menu_file.setheadertitle ("File operation");//set Menu TitleMenu_file.setheadericon (R.mipmap.ic_launcher);//set Menu icon//Create a submenu for the Edit menuMenu_edit.add (2, 101, 1, "Copy"); Menu_edit.add (2, 102, 1, "Paste"); Menu_edit.add (2, 103, 1, "cut"); Menu_edit.setheadertitle ("Edit operation");//set Menu TitleMenu_edit.setheadericon (R.mipmap.ic_launcher);//set Menu icon        return true; }    /*** Action triggered after clicking the submenu button*/@Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getgroupid ()) { Case1:                //Click on the first set of submenus, corresponding to the first set of sub-menu buttons                Switch(Item.getitemid ()) { Case101: Toast.maketext ( This, "clicked on new", Toast.length_short). Show ();  Break;  Case102: Toast.maketext ( This, "clicked on Open", Toast.length_short). Show ();  Break;  Case103: Toast.maketext ( This, "click Save", Toast.length_short). Show ();  Break; }                 Break;  Case2:                //Click on the second set of submenus, corresponding to the second set of sub-menu buttons                Switch(Item.getitemid ()) { Case101: Toast.maketext ( This, "click Copy", Toast.length_short). Show ();  Break;  Case102: Toast.maketext ( This, "Click Paste", Toast.length_short). Show ();  Break;  Case103: Toast.maketext ( This, "clicked Cut", Toast.length_short). Show ();  Break; }                 Break; }        return Super. onoptionsitemselected (item); }}

2. Create a submenu from XML

To generate a sub-menu file

Menu.xml

<Menuxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Tools:context=". Mainactivity ">    <ItemAndroid:title= "File"app:showasaction= "Never">        <Menu>            <ItemAndroid:id= "@+id/menu_new"Android:title= "new"/>            <ItemAndroid:id= "@+id/menu_open"Android:title= "Open" />            <ItemAndroid:id= "@+id/menu_save"Android:title= "Save" />            <ItemAndroid:id= "@+id/menu_exit"Android:title= "Exit" />        </Menu>    </Item>    <ItemAndroid:title= "Edit"app:showasaction= "Never">        <Menu>            <ItemAndroid:id= "@+id/menu_copy"Android:title= "Copy" />            <ItemAndroid:id= "@+id/menu_paste"Android:title= "Paste" />            <ItemAndroid:id= "@+id/menu_cut"Android:title= "Cut" />        </Menu>    </Item></Menu>

Main.java: Background code files

 PackageCom.example.zhengcheng.submenudemo;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.Menu;ImportAndroid.view.MenuInflater;ImportAndroid.view.MenuItem;ImportAndroid.widget.Toast; Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//Import XML Menu ContentsMenuinflater Inflater =Getmenuinflater ();        Inflater.inflate (R.menu.menu_main, menu); return true; }    /*** Action triggered after clicking the submenu button*/@Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseR.id.menu_new:toast.maketext ( This, "clicked on new", Toast.length_short). Show ();  Break;  CaseR.id.menu_open:toast.maketext ( This, "clicked on Open", Toast.length_short). Show ();  Break;  CaseR.id.menu_save:toast.maketext ( This, "click Save", Toast.length_short). Show ();  Break;  CaseR.id.menu_exit:toast.maketext ( This, "clicked Exit", Toast.length_short). Show ();                Finish ();  Break;  CaseR.id.menu_copy:toast.maketext ( This, "click Copy", Toast.length_short). Show ();  Break;  CaseR.id.menu_paste:toast.maketext ( This, "Click Paste", Toast.length_short). Show ();  Break;  CaseR.id.menu_cut:toast.maketext ( This, "clicked Cut", Toast.length_short). Show ();  Break; }        return Super. onoptionsitemselected (item); }}

Android Learning (23) Submenu submenu

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.