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.