Use of the menu level menu, level two menus for Android learning notes

Source: Internet
Author: User

(1) Layout file not changed

(2) The code in the Main.xml file in the Res--menu directory is as follows:

<menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <!--Level menu--<item Android                 : id= "@+id/file" android:title= "@string/file" > <!--Level Two menu--<menu> <item            Android:id= "@+id/create" android:title= "@string/create" > </item> <item android:id= "@+id/open" android:title= "@string/open" > &LT;/ITEM&G        T </menu> </item> <!---level menu-<item android:id= "@+id/chioce" android:title= "@s Tring/chioce "> <!--Level Two menu--<menu> <item android:id=" @+id/creat E1 "android:title=" @string/create "> </item> <item android: Id= "@+id/open1" android:title= "@string/open" > </item> </menu> </item ></menu&Gt 

(3) in the String.xml file under the values directory:

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "App_name" >menu_submenu</ string>    <string name= "action_settings" >Settings</string>    <string name= "Hello_world" >hello world!</string>    <string name= "file" > File </string>    <string name= "Create" > New </string>    <string name= "open" > Open </string>    <string name= "Chioce" > Options </string ></resources>

(3) The Mainactivity.java code is as follows:

Package Com.example.menu_submenu;import Android.os.bundle;import Android.app.activity;import Android.content.intent;import Android.view.menu;import Android.view.menuitem;import Android.view.SubMenu;import Android.widget.toast;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} /* * Processing level Menu */@Overridepublic boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case R.id.file: Toast.maketext (Mainactivity.this, "level menu file:", Toast.length_short). Show (); Break;case R.id.chioce:toast.maketext ( Mainactivity.this, "level menu Chioce ...", Toast.length_short). Show (); break;default:break;} return super.onoptionsitemselected (item);} /* * Processing Level two menu */@Overridepublic Boolean OnmenuitemseleCTED (int featureid, MenuItem item) {switch (Item.getitemid ()) {case R.id.create1:toast.maketext (mainactivity.this, " Process level Two menu create1: ", Toast.length_short). Show (); Break;case R.id.open1:toast.maketext (Mainactivity.this," Process level Two menu create1: ", Toast.length_short). Show (); Break;case R.id.create:toast.maketext (Mainactivity.this," Process level Two menu create: ", Toast.length_short). Show (); Break;case R.id.open:toast.maketext (mainactivity.this," Processing level two menu open ... " , Toast.length_short). Show (); break;default:break;} Return super.onmenuitemselected (Featureid, item);}}


Use of the menu level menu, level two menus for Android learning notes

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.