Each activity in Android is bundled with a optionmenu, which is initialized by invoking the Oncreateoptionmenu (Menu menu) function, which we can override to set a response menu for the activity. Here's a small example of how this is done.
PackageCom.yangzi.optionmenu;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;/* * After creating an Android project, go to the Mainactivity class, overwrite the Oncreateoptionsmenu method and onoptionitemselected method here * */ Public class mainactivity extends Activity { //Declaration menu sub-option ID number Private Static Final intITEM1 = Menu.first;Private Static Final intITEM2 = item1+1;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); }/ * Override this method to generate a menu * * / @Override Public Boolean Oncreateoptionsmenu(Menu menu) {the//add method is to add a submenu of the submenu /* For parameter description of the Add method: * Parameter one: Group ID (int): Set ID to indicate which group the sub-option belongs to * parameter two: item ID (int): Sub-option ID, which is used to indicate the sub-option * Parameter three: order ID (int): Display order, indicating the display order of the sub-option, default to 0, indicating in addition order * parameter four: title (String): Used to define the display text of the sub-option * * */Menu.add (0, ITEM1,0,"Start"); Menu.add (0, ITEM2,0,"Exit");return true; }/ * Overwrite the method and add the corresponding menu event * */ @Override Public Boolean onoptionsitemselected(MenuItem Item) {/* * Depending on the selected sub-option (differentiated by item ID), different responses are made * * */ Switch(Item.getitemid ()) { CaseItem1:settitle ("Start the game");//The title of the activity is set here Break; CaseItem2:settitle ("Exit"); Break; }return true; }}
If you want to see the effect, try it yourself (-_-)
Options menu in Android (optionmenu) use case