The implementation method of menu key function of Android programming _android

Source: Internet
Author: User

The example of this article describes the implementation of menu key function of Android programming. Share to everyone for your reference. The specific analysis is as follows:

The Android app can eject a menu with menu keys and now pops a menu with two options through the Menus button. Click on the first button, the text box will display the "1th button", click the second button, the text box will display "2nd button"

Package com.test_menu;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import android.view.*;
import android.widget.*;;
 public class Test_menu extends activity {public static final int ITEM0 = Menu.first;
 public static final int ITEM1 = Menu.first + 1;
 private TextView text; /** called the activity is a.
  * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main);
 Text = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
  @Override public boolean Oncreateoptionsmenu (Menu menu) {super.oncreateoptionsmenu (menu);
  Menu.add (0,item0,0, "Button1");
  Menu.add (0,item1,0, "button2");
  Menu.finditem (ITEM1);
 return true; public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case ITEM0: {Text.settex
   T ("1th button");
  Break
   Case ITEM1: {text.settext ("2nd button");
  Break } return super.onoptionsitemselected (iTEM); }
}

Run Effect chart:

I hope this article will help you with your Android program.

Related Article

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.