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:

Show or hide all menu items. Use setGroupVisible ()
Enable or disable all menu items. Use setGroupEnabled ()
Specify whether all menu items are checkable. Use setGroupCheckable ()
To create a group, you can nest the <item> element in the resource file to a <group> element, 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"?>
<Menu xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: id = "@ + id/item1"
Android: icon = "@ drawable/item1"
Android: title = "@ string/item1"/>
<! -- Menu group -->
<Group android: id = "@ + id/group1">
<Item android: id = "@ + id/groupItem1"
Android: title = "@ string/groupItem1"/>
<Item android: id = "@ + id/groupItem2"
Android: title = "@ string/groupItem2"/>
</Group>
</Menu>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: id = "@ + id/item1"
Android: icon = "@ drawable/item1"
Android: title = "@ string/item1"/>
<! -- Menu group -->
<Group android: id = "@ + id/group1">
<Item android: id = "@ + id/groupItem1"
Android: title = "@ string/groupItem1"/>
<Item android: id = "@ + id/groupItem2"
Android: title = "@ string/groupItem2"/>
</Group>
</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 www.2cto.com


 

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: a checkbox or radio button cannot be displayed for the icon menu item (from the option menu. 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.

By using the android: checkable attribute in the <item> element, you can define the checkable behavior for different menu items, or specify android for the <group> element: the checkableBehavior attribute defines 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"?>
<Menu xmlns: android = "http://schemas.android.com/apk/res/android">
<Group android: checkableBehavior = "single">
<Item android: id = "@ + id/red"
Android: title = "@ string/red"/>
<Item android: id = "@ + id/blue"
Android: title = "@ string/blue"/>
</Group>
</Menu>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: android = "http://schemas.android.com/apk/res/android">
<Group android: checkableBehavior = "single">
<Item android: id = "@ + id/red"
Android: title = "@ string/red"/>
<Item android: id = "@ + id/blue"
Android: title = "@ string/blue"/>
</Group>
</Menu>

Property android: checkableBehavior: the 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 use the android: checked attribute in the <item> element to specify a default checked state for the menu item and use the setChecked () method in the code to change its checked state.

When a checkable menu item is selected, the system calls the corresponding callback method (such as onOptionsItemSelected ()). in this method, you must specify the checkbox status. because a checkbox or radio button does not automatically change its status. you can use isChecked () to query the current status of a menu item. You can use setChecked () to set the checked status. for example:

@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case R. id. vibrate:
Case R. id. dont_vibrate:
If (item. isChecked () item. setChecked (false );
Else item. setChecked (true );
Return true;
Default:
Return super. onOptionsItemSelected (item );
}
}
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case R. id. vibrate:
Case R. id. dont_vibrate:
If (item. isChecked () item. setChecked (false );
Else item. setChecked (true );
Return true;
Default:
Return super. onOptionsItemSelected (item );
}
}
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 only be used 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.

 

From the column of nkmnkm
 

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.