OptionsMenu
In The android SDK, options menu is explained as follows: Options menus: the icon menus do not support item check marks and only show The item's condensed title. the expanded menus (only available if six or more menu items are visible, reached via the 'more' item in the icon menu) do not show item icons, and item check marks are discouraged.
The specific operation is the menu displayed when you press the menu key on the android phone. You can click the corresponding menu to perform the corresponding operation.
Procedure
Option menu is easy to use. First, create a menu directory under the res folder, and then add the menu XML file to it.
[Html]
? Xml version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: id = "@ + id/settings" android: title = "@ string/settings_label"
Android: icon = "@ android: drawable/ic_menu_preferences"
Android: alphabeticShortcut = "s">
</Item>
</Menu>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: id = "@ + id/settings" android: title = "@ string/settings_label"
Android: icon = "@ android: drawable/ic_menu_preferences"
Android: alphabeticShortcut = "s">
</Item>
</Menu> item: each item corresponds to a menu.
Android: icon: the icon used to specify the menu display.
Android: title: the title of the menu, which is displayed under the icon.
Android: alphabeticShortcut: shortcut key selected from the menu.
For more Menu attributes, see Menu Resource on the SDK.
The second step is to rewrite the onCreateOptionsMenu method in the Activity and convert the xml resource to the Menu instance through MenuInflater. And displayed in Activity
[Java]
/**
* Create a system Menu, which is displayed by pressing the Menu key
*/
@ Override
Publicboolean onCreateOptionsMenu (Menu menu ){
Super. onCreateOptionsMenu (menu );
// Use MenuInflater to instantiate XML as a Menu Object
MenuInflater inflater = getMenuInflater ();
Inflater. inflate (R. menu. menu, menu );
Returntrue;
}
/**
* Create a system Menu, which is displayed by pressing the Menu key
*/
@ Override
Publicboolean onCreateOptionsMenu (Menu menu ){
Super. onCreateOptionsMenu (menu );
// Use MenuInflater to instantiate XML as a Menu Object
MenuInflater inflater = getMenuInflater ();
Inflater. inflate (R. menu. menu, menu );
Returntrue;
} Run the simulator and click the Menu key to view the display of the Menu directory. However, clicking the menu does not respond, because we have not added the corresponding event processing. To add an event to the menu, you must override the public boolean onOptionsItemSelected (MenuItem item) method.
[Java]
/**
* Menu Click Event
*/
@ Override
Publicboolean onOptionsItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case R. id. about:
Logger. d ("Displaying the about box ");
// Display the page
DisplayAboutBox ();
Returntrue;
Case R. id. help:
Logger. d ("Displaying the help dialog ");
// The help account page is displayed.
DisplayHelpDialog ();
Returntrue;
Case R. id. settings:
Logger. d ("Displaying the settings ");
// Jump to the configuration page
DisplaySettings ();
Returntrue;
Case R. id. teams:
Logger. d ("Displaying the team configuration ");
// Jump to the team configuration page
DisplayTeamConfiguration ();
Returntrue;
Case R. id. quit:
Logger. d ("Quitting ");
// Exit the program
Finish ();
Returntrue;
Default:
Logger. e ("Unknown menu item selected ");
// Return False to the system's normal processing menu, and return True to the program for processing
Returnfalse;
}
}
/**
* Menu Click Event
*/
@ Override
Publicboolean onOptionsItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case R. id. about:
Logger. d ("Displaying the about box ");
// Display the page
DisplayAboutBox ();
Returntrue;
Case R. id. help:
Logger. d ("Displaying the help dialog ");
// The help account page is displayed.
DisplayHelpDialog ();
Returntrue;
Case R. id. settings:
Logger. d ("Displaying the settings ");
// Jump to the configuration page
DisplaySettings ();
Returntrue;
Case R. id. teams:
Logger. d ("Displaying the team configuration ");
// Jump to the team configuration page
DisplayTeamConfiguration ();
Returntrue;
Case R. id. quit:
Logger. d ("Quitting ");
// Exit the program
Finish ();
Returntrue;
Default:
Logger. e ("Unknown menu item selected ");
// Return False to the system's normal processing menu, and return True to the program for processing
Returnfalse;
}
} Obtain the ID of the corresponding menu item based on the getItemId () of MenuItem, which corresponds to android: id in xml. In this way, the menu function is implemented. We recommend that you add some unused function links to the menu, such as about us and help, because you may not know how to use the menu.