Android menu details 5: Other menu features

Source: Internet
Author: User

Here are some other features that you can apply to most menu entries.

Menu Group

A menu group is composed of a series of menu items that share certain features. For a group, you can:

  • Display or hide all menu items. UsagesetGroupVisible()
  • How to Use enable or disable all menu itemssetGroupEnabled()
  • Specifies whether all menu items are checkable. UsagesetGroupCheckable()

To create a group, you can<item>Element nesting to<group>Or specify a group ID when calling the add () method.

The following is an example of using a group in a menu resource:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <menu xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: Id = "@ + ID/Item1" <br/> Android: icon = "@ drawable/Item1" <br/> Android: Title = "@ string/Item1"/> <br/> <! -- Menu group --> <br/> <group Android: Id = "@ + ID/group1"> <br/> <item Android: id = "@ + ID/groupitem1" <br/> Android: Title = "@ string/groupitem1"/> <br/> <item Android: id = "@ + ID/groupitem2" <br/> Android: title = "@ string/groupitem2"/> <br/> </group> <br/> </menu>
The menu items in the group are essentially the same as those in the non-group menu items-all three menu items are still brother. however, you can use the methods listed above to modify some attributes of the two menu items in the Group based on the group ID.

Checkable menu item

Figure 3.Screenshot of a submenu with checkableitems.

A menu can be used as a user interface to open or close an option. In this case, a checkbox is used for independent options, or a column of radio buttons is used for selecting one or more options. demonstrate the appearance of the sub-menu of the checkable menu item with the radio button.

Note: Icon menu items(From the option menu) a checkbox or radio button cannot be displayed. if you want to use the icon menu item to be checkable, you must manually specify the checked status and switch its icon or text every time the status changes.

In<item>Use in elementandroid:checkableProperties, you can define their checkable behavior for different menu items, or<group>Element specifiedandroid:checkableBehaviorAttribute to define the checkable behavior of all menu items in the group. For example, all menu items in this group have the radio checkable attribute:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <menu xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <group Android: checkablebehavior = "single"> <br/> <item Android: id = "@ + ID/red" <br/> Android: Title = "@ string/red"/> <br/> <item Android: id = "@ + ID/Blue" <br/> Android: title = "@ string/Blue"/> <br/> </group> <br/> </menu>

Attributeandroid:checkableBehaviorThe following values are acceptable:
single
Only one menu item in the group can be checked (radio button)
all
All menu items in the group can be checked (checkboxes)
none
All menu items in the Group cannot be checked

You can<item>Use in elementandroid:checkedAttribute specifies a default checked status for the menu item and can be used in code.setChecked()Method to change its checked status.

When a checkable menu item is selected, the system calls the corresponding callback method (for exampleonOptionsItemSelected()). In this method, you must specify the status of the checkbox, because a checkbox or radio
The button does not automatically change its status. You can useisChecked()Query the current status of a menu item. You can usesetChecked()Set the checked status. For example:

@ Override <br/> Public Boolean onoptionsitemselected (menuitem item) {<br/> switch (item. getitemid () {<br/> case R. id. vibrate: <br/> case R. id. dont_vibrate: <br/> If (item. ischecked () item. setchecked (false); <br/> else item. setchecked (true); <br/> return true; <br/> default: <br/> return Super. onoptionsitemselected (item); <br/>}< br/>}
If you set the status as described above, the visual status of the menu item will not change. however, if you do, the activity will save the checked status of the menu item, so when the user opens the menu later, the checked status you set will be seen.

Note:The checkable menu item is intended to be used only in a single session, and its status will not be saved after the program is closed. If you have program settings to save, you must use shared preferences to save the data.

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.