将单选按钮添加到ContextMenu [英] Adding RadioButtons to ContextMenu

查看:105
本文介绍了将单选按钮添加到ContextMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在上下文菜单中添加单选按钮,但不确定如何.它是这样创建的:

I would like to add radio buttons to my context menu, but I'm not sure how. This is how it is created:

@Override  
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  

        super.onCreateContextMenu(menu, v, menuInfo);  
        menu.setHeaderTitle("Selection Options");  
        menu.add(0, v.getId(), 0, "Remove");  
    }  

推荐答案

来自您的代码:

menu.add(0, v.getId(), 0, "Remove"); 

v是一个视图,可以是RadioButton或任何其他类型的视图.

v is a View that can be a RadioButton or any other type of Views.

如果您没有使用xml定义RadioButton.您应该在应用程序中设置其ID.

if you are not using xml to define RadioButton. you should set its ID in your application.

v.setId();

v.setId();

或者您可以在res/values中定义ids.xml.

Or you can define ids.xml in res/values.

samples/ApiDemos/src/com/example/android/apis/RadioGroup1.java samples/ApiDemp/res/values/ids.xml

samples/ApiDemos/src/com/example/android/apis/RadioGroup1.java samples/ApiDemp/res/values/ids.xml

菜单和上下文菜单开发人员指南:

Menu & Context Menu developers guide:

http://developer.android.com/guide/topics/ui /menus.html

如果滚动以上页面,则可以在上下文菜单中找到RadioButton示例.

if you scroll the above page you can find RadioButton sample in context menu.

在上面的页面中引用:

可检查的菜单项

Checkable menu items

菜单可以用作界面 用于打开和关闭选项,使用 独立选项的复选框,或者 相互分组的单选按钮 专有选项.图2显示了一个 带有可检查项目的子菜单 带有单选按钮.

A menu can be useful as an interface for turning options on and off, using a checkbox for stand-alone options, or radio buttons for groups of mutually exclusive options. Figure 2 shows a submenu with items that are checkable with radio buttons.

注意:图标菜单中的菜单项 (从选项菜单中)无法显示 复选框或单选按钮.如果你 选择在图标菜单中制作项目 检查中,您必须手动指出 通过交换图标来检查状态 和/或在每次状态时输入文字 变化.

Note: Menu items in the Icon Menu (from the Options Menu) cannot display a checkbox or radio button. If you choose to make items in the Icon Menu checkable, you must manually indicate the checked state by swapping the icon and/or text each time the state changes.

您可以定义可检查的行为 对于单个菜单项,使用 的android:checkable属性 元素,或整个组 与android:checkableBehavior 元素中的属性.为了 例如,此菜单组中的所有项目 可以通过单选按钮进行检查:

You can define the checkable behavior for individual menu items using the android:checkable attribute in the element, or for an entire group with the android:checkableBehavior attribute in the element. For example, all items in this menu group are checkable with a radio button:

> <?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> The android:checkableBehavior attribute

可以接受:

accepts either:

单个组中只有一项 可以检查(单选按钮)全部全部 可以选中项目(复选框)无 没有项目可检查,您可以申请 默认检查状态为使用 的android:checked属性 元素并在代码中对其进行更改 使用setChecked()方法.

single Only one item from the group can be checked (radio buttons) all All items can be checked (checkboxes) none No items are checkable You can apply a default checked state to an item using the android:checked attribute in the element and change it in code with the setChecked() method.

选择了可检查的项目后, 系统调用您各自的 项选择的回调方法(例如 onOptionsItemSelected()).是这里 您必须设置状态 复选框,因为有一个复选框或单选 按钮不会更改其状态 自动地.您可以查询 项目的当前状态(原为 在用户选择之前) isChecked(),然后设置选中的 setChecked()进入状态.例如:

When a checkable item is selected, the system calls your respective item-selected callback method (such as onOptionsItemSelected()). It is here that you must set the state of the checkbox, because a checkbox or radio button does not change its state automatically. You can query the current state of the item (as it was before the user selected it) with isChecked() and then set the checked state with setChecked(). 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);   }
> } 

如果您未设置选中状态

这样,那么可见状态 该项目(复选框或单选 按钮),当用户 选择它.设置状态后, 活动保留选中的 项目的状态,以便当 用户稍后打开菜单,选中 您设置的状态是可见的.

this way, then the visible state of the item (the checkbox or radio button) will not change when the user selects it. When you do set the state, the Activity preserves the checked state of the item so that when the user opens the menu later, the checked state that you set is visible.

注意:可检查的菜单项是 旨在仅用于 每次会话的基础,之后不保存 该应用程序已被破坏.如果你 有您需要的应​​用程序设置 想要为用户保存,您 应该使用共享存储数据 偏好设置.

Note: Checkable menu items are intended to be used only on a per-session basis and not saved after the application is destroyed. If you have application settings that you would like to save for the user, you should store the data using Shared Preferences.

这篇关于将单选按钮添加到ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆