将单选按钮添加到上下文菜单

发布于 2024-09-17 12:37:55 字数 337 浏览 3 评论 0原文

我想将单选按钮添加到我的上下文菜单中,但我不知道如何添加。它是这样创建的:

@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");  
    }  

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");  
    }  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不如归去 2024-09-24 12:37:55

从您的代码中:

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

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

如果您不使用 xml 来定义 RadioButton。你应该在你的应用程序中设置它的ID。

v.setId();

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

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

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

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

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

引用以上页面:

可检查的菜单项

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

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

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

> <?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

接受以下任一:

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

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

> @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);   }
> } 

如果不设置checked状态

这样,可见状态
项目(复选框或单选框
按钮)在用户使用时不会改变
选择它。当你设置状态时,
该活动保留了检查的
该项目的状态,以便当
用户稍后打开菜单,选中的
您设置的状态可见。

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

from your code:

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

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

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

v.setId();

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

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

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

Quote form the above page:

Checkable menu items

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.

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:

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.

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);   }
> } 

If you don't set the checked state

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.

避讳 2024-09-24 12:37:55
RadioButton rBtn1 = new RadioButton(this);
RadioButton rBtn2 = new RadioButton(this);
rBtn1.setText("radio button 1");
rBtn2.setText("radio button 2");
//Add all your RadioButtons the same way.

RadioGroup group = new RadioGroup(this);
group.addView(rBtn1);
group.addView(rBtn2);

menu.add(0, group.getId(), 0, "whatever");

我没有尝试过,但我希望它能起作用:

RadioButton rBtn1 = new RadioButton(this);
RadioButton rBtn2 = new RadioButton(this);
rBtn1.setText("radio button 1");
rBtn2.setText("radio button 2");
//Add all your RadioButtons the same way.

RadioGroup group = new RadioGroup(this);
group.addView(rBtn1);
group.addView(rBtn2);

menu.add(0, group.getId(), 0, "whatever");

I didn't try it but I hope it works:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文