The 12th chapter, the menu small demo (Android)

Source: Internet
Author: User

In the Res folder, under the menu folder, such as (not new), build Main.xml

<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <!--NOTE: The ID cannot be a full digital--    < Item        android:id= "@+id/item001"        android:title= "001"/>    <item        android:id= "@+id/item002"        android:title= "002"/></menu>

Mainactivity.java

Package Com.example.demo15;import Android.app.activity;import Android.os.bundle;import android.util.Log;import Android.view.menu;import Android.view.menuitem;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} Create Menu@overridepublic Boolean Oncreateoptionsmenu (Menu menu) {//Use this method to populate the Getmenuinflater (). Inflate (R.menu.main, menu); return true;} Each option is selected @overridepublic Boolean onoptionsitemselected (MenuItem Item) {//TODO auto-generated method stub// Switch is also available here to write if (item.getitemid () = = r.id.item001) {log.i ("Log", "do something ...");} else if (item.getitemid () = = r.id.item002) {log.i ("Log", "do something ...");} return super.onoptionsitemselected (item);}}

Run:


Click on the system's "menu" button:


The 12th chapter, the menu small demo (Android)

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.