This article describes the Android programming implementation to add a menu to the application method. Share to everyone for your reference, specific as follows:
There are many ways to add menus, and it is generally recommended that you create menus with XML.
To establish the menu step:
Create a menu folder under Res and add an XML file inside the menu file:
<?xml version= "1.0" encoding= "Utf-8"?> <menu xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<item android:id=" @+id/play android:title= "Play
"
android:visible= "true"/>
<item android:id= "@+id/stop"
android:title= "Stop"
android:visible= "true"/>
</menu>
To add a menu to an application:
How do I add a defined menu when the application starts? In the Oncreateoptionsmenu () event, use the Menuinflater to add the defined menu to the program:
@Override Public
Boolean oncreateoptionsmenu (Menu menu) {
//TODO auto-generated method stub
Menuinflater Inflater = Getmenuinflater ();
Inflater.inflate (R.menu.options_menu, menu);
return true;
}
At this point, the menu has been added to the application, but there is a problem, now the menu is only displayed, and does not handle the trigger menu message, then you can use the onoptionsitemselected () event to customize message processing , The following is a menu implementation that stops and plays music:
@Override Public
Boolean onoptionsitemselected (MenuItem item) {
//TODO auto-generated method stub
int item_id = Item.getitemid ();
Switch (item_id) {case
r.id.play:
Intent Intent = new Intent (webtestactivity.this, yypservice.class);
StartService (intent);
break;
Case R.id.stop:
this.onstop ();
break;
Default: Return
false;
}
return true;
}
The effect of the program is as follows:
I hope this article will help you with the Android program.