My Android Learning experience 33

Source: Internet
Author: User

Add a menu to activity

1. Create a new folder under the Res directory menu

Right-click Res, select New->folder,folder Name to write as menu

2. Create a new XML file under the new menu directory

Right-click menu, click New->android XML File, fill in the popup box with a name, such as main, click Finish

3. In the new Main.xml file, write the menu item you want item, as follows:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Menuxmlns:android= "Http://schemas.android.com/apk/res/android" >3     <ItemAndroid:id= "@+id/add_item"4 Android:title= "Add"/>5     <ItemAndroid:id= "@+id/remove_item"6 Android:title= "Remove"/>7 </Menu>

4. Return to main activity

Rewrite two methods Oncreateoptionsmenu (Menu menu) and onoptionsitemselected (MenuItem item);

The first is to add a menu and the second is to respond when the corresponding menu item is clicked

 Public BooleanOncreateoptionsmenu (Menu menu) {//TODO auto-generated Method Stubgetmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//TODO auto-generated Method Stub        Switch(Item.getitemid ()) { CaseR.id.add_item:toast.maketext ( This, "ADD", Toast.length_short). Show ();  Break;  CaseR.id.remove_item:toast.maketext ( This, "REMOVE", Toast.length_short). Show ();  Break; default:             Break; }        return true; }

5. Operation effect

My Android Learning experience 33

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.