Android to create Optionsmenu method _android

Source: Internet
Author: User

The example in this article describes how Android creates Optionsmenu. Share to everyone for your reference. The implementation methods are as follows:

First, with XML configuration

Create a new menu folder under the Res folder, create a new Options_menu.xml file under menu, and add code

1,

<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:id= "@+id/about"
android:title= "about"
android:checkable= "true"/>
<item android:id= "@+id/choose"
android: title= "select"/>
<item android:id= "@+id/quit" android:title=
"quit"/>
</menu>

2, the Activity class to add code

public boolean Oncreateoptionsmenu (Menu menu) {menuinflater inflater = Getmenuinflater ();
 Inflater.inflate (R.menu.options_menu, menu);
 return Super.oncreateoptionsmenu (menu);
 public boolean onoptionsitemselected (MenuItem aitem) {super.onoptionsitemselected (AItem);
 Switch (Aitem.getitemid ()) {Case R.id.about:settitle ("You pressed the ' About ' menu item");
 Progressbarhorizontal.setprogress (200);
 Openoptionsdialog ();
 Break
 Case R.id.choose:settitle ("You pressed the ' Select ' Menu");
 Openoptionschoosedialog ();
 Break
 Case R.id.quit:settitle ("you press the ' Leave ' menu item");
 Finish ();
 Break
 return true; private void Openoptionsdialog () {new Alertdialog.builder (this). Settitle (R.string.app_about). Setmessage (r.string . app_about_msg). Setpositivebutton (R.STRING.STR_OK, New Dialoginterface.onclicklistener () {public void OnClick (
 Dialoginterface dialoginterface, int i) {}}). Show (); private void Openoptionschoosedialog () {new Alertdialog.builder (this). Settitle (R.string.str_alert). Setitems (R. Array.items_irdc_dialog, New Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialoginterface
 , int i) {/* string[] Aryshop = Getresources (). Getstringarray (R.array.items_irdc_dialog); New Alertdialog.builder (this). Setmessage (r.string.app_about_msg). Setpositivebutton (R.STRING.STR_OK, new 
 Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialoginterface, int i) {}}]. Show ();
). Show ();

 }

Ok...

Second, directly on the code:

public static final int about_id = Menu.first;
public static final int choose_id = Menu.first + 1;
public static final int exit_id = Menu.first + 2;
 public boolean Oncreateoptionsmenu (Menu menu) {menu.add (0, about_id, 0, r.string.app_about);
 Menu.add (0, choose_id, 0, R.string.str_choose);
 Menu.add (0, exit_id, 0, R.string.str_exit);
 return Super.oncreateoptionsmenu (menu);
 public boolean onoptionsitemselected (MenuItem aitem) {super.onoptionsitemselected (AItem);
 Switch (Aitem.getitemid ()) {Case About_id:settitle ("You pressed the ' About ' menu item");
 Progressbarhorizontal.setprogress (200);
 Openoptionsdialog ();
 Break
 Case Choose_id:settitle ("You pressed the ' Select ' Menu");
 Openoptionschoosedialog ();
 Break
 Case Exit_id:settitle ("you press the ' Leave ' menu item");
 Finish ();
 Break
 return true; private void Openoptionsdialog () {new Alertdialog.builder (this). Settitle (R.string.app_about). Setmessage (r.string . app_about_msg). Setpositivebutton (R.STRING.STR_OK, New Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialoginterface, int i) {}}). Show (); private void Openoptionschoosedialog () {new Alertdialog.builder (this). Settitle (R.string.str_alert). Setitems (R.ar Ray.items_irdc_dialog, New Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialoginterface,
 int i) {/* string[] Aryshop = Getresources (). Getstringarray (R.array.items_irdc_dialog); New Alertdialog.builder (this). Setmessage (r.string.app_about_msg). Setpositivebutton (R.STRING.STR_OK, new 
 Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialoginterface, int i) {}}). Show ();
). Show ();

 }

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.