Optionsmenu of Android

Source: Internet
Author: User
Preface standuptimer, an open-source project on the Android platform during this period, is a simple Android Application designed by jwood to control the conference time, similar to the stopwatch countdown. The options menu in the android SDK of optionsmenu is explained as follows: Options menus : Icon menus Do not support item check marks and only show the item's Condensed title . 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. To use option menu, you can create a menu directory in the res folder and add the XML file of the menu to it. <? 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 /**
* Create a system menu, which is displayed by pressing the menu key
*/
@ Override
Public Boolean Oncreateoptionsmenu (menu ){
Super . Oncreateoptionsmenu (menu );
// Instantiate XML as a menu object through menuinflater
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. /**
* Menu Click Event
*/
@ Override
Public Boolean Onoptionsitemselected (menuitem item ){
Switch (Item. getitemid ()){
Case R. Id. About:
Logger. D ( " Displaying the about box " );
// Show about page
Displayaboutbox ();
Return True ;
Case R. Id. Help:
Logger. D ( " Displaying the help Dialog " );
// Displayed on the Help Page
Displayhelpdialog ();
Return True ;
Case R. Id. settings:
Logger. D ( " Displaying the settings " );
// Go to configuration page
Displaysettings ();
Return True ;
Case R. Id. teams:
Logger. D ( " Displaying the team configuration " );
// Jump to the team configuration page
Displayteamconfiguration ();
Return True ;
Case R. Id. Quit:
Logger. D ( " Quitting " );
// ExitProgram
Finish ();
Return True ;
Default :
Logger. E ( " Unknown menu item selected " );
// Return false to the system's normal processing menu, and return true to the program for processing
Return False ;
}

}

 

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.

Series Indexes
Android open-source project-standuptimer learning notes Index
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.