Android menu description 6: shortcut keys and menu intent

Source: Internet
Author: User
Shortcut Key

To speed up operations on the option menu, you can add shortcut keys to the menu on devices with physical buttons. Shortcut Keys can correspond to letters or numbers on the keyboard. What you need to do is<item>Attribute specified by elementandroid:alphabeticShortcutAndandroid:numericShortcutValue. You can also use the method in the code.setAlphabeticShortcut(char)
AndsetNumericShortcut(char). To complete. The shortcut key is not case sensitive.

For example, if you use the "S" key as the shortcut key for the menu item "save", when the menu is opened, the user presses the "S" key, the "save" menu item is selected.

The shortcut key appears at the bottom of the menu item name as a tip (unless the menu is an icon menu, it can only appear when the user presses the "menu" key ).

Note: The shortcut key can only work on a device with a physical keyboard and cannot be used in the context menu.

Add menu intents dynamically

Sometimes you may want to use intent to start a activiry (whether this activity is in your own program or another program ). if you know the required intent, You can execute the intentstartActivity()Method complete.

However, if you are not sure that the user device has a program that responds to this intent, the added menu items may be redundant. to solve this problem, Android allows you to dynamically add menu items when you find activity with all the targeted intent responses.

To add a menu item based on the activity that responds to the destination intent, You need:

  1. Define a categoryCATEGORY_ALTERNATIVEAnd/orCATEGORY_SELECTED_ALTERNATIVEOf course, you can also add other categories as needed.
  2. CallMenu.addIntentOptions().
    Android finds the program that can be executed and adds it to your menu.

In this way, if a program with no full intent exists, no menu item will be added.

Note:CATEGORY_SELECTED_ALTERNATIVEIt is used to process the selected elements on the screen. Therefore, it can only be used for menus created in oncreatecontextmenu.

For example:

@ Override <br/> Public Boolean oncreateoptionsmenu (menu) {<br/> super. oncreateoptionsmenu (menu); </P> <p> // create an intent that describes the requirements to fulfill, to be encoded <br/> // in our menu. the offering app must include a category value of intent. category_alternative. <br/> intent = new intent (null, datauri); <br/> intent. addcategory (intent. category_alternative); </P> <p> // search and populate the menu with acceptable offering applications. <br/> menu. addintentoptions (<br/> r. id. intent_group, // menu group to which new items will be added <br/> 0, // unique item ID (none) <br/> 0, // order for the items (none) <br/> This. getcomponentname (), // The current activity name <br/> null, // specific items to place first (none) <br/> intent, // intent created above that describes our requirements <br/> 0, // additional flags to control items (none) <br/> null ); // array of menuitems that correlate to specific items (none) </P> <p> return true; <br/>}

Each time a corresponding intent activity is found, a menu item is added.android:labelAs the title of the menu item, the program icon is used as the icon of the menu item.addIntentOptions()
Returns the number of added menu items.

Note: When calling addIntentOptions()The parameter menu
Replace all menu items in the group.

Allow your activity to be added to other menus

You can also provide your activity service to other programs, so your program can be included in the menus of other programs (which is reversed in the previous section ).

To be included in menus of other programs, you need to define an intent filter, but this filter must includeCATEGORY_ALTERNATIVEAnd/orCATEGORY_SELECTED_ALTERNATIVE
For example:

<Intent-filter label = "resize image"> <br/>... <br/> <category Android: Name = "android. intent. category. alternative "/> <br/> <category Android: Name =" android. intent. category. selected_alternative "/> <br/>... <br/> </intent-filter>

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.