如何设置RadioGroup中只有一个RadioButton可以同时选中

2023-12-30

我在radiogroup中创建了一个单选按钮,但是当我尝试运行应用程序时,所有单选按钮都可以一直被选择,如何设置一次只能选择一个单选按钮?

我在用着Fragment

RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.RGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // find which radio button is selected
                if(checkedId == R.id.Abdominal) {
                    Toast.makeText(getActivity().getApplicationContext(), "choice: A",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Arm) {
                    Toast.makeText(getActivity().getApplicationContext(), "choice: B",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Back){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: C",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Chest){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: D",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Leg){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: E",
                            Toast.LENGTH_SHORT).show();
                } else if(checkedId == R.id.Shoulder){
                    Toast.makeText(getActivity().getApplicationContext(), "choice: F",
                            Toast.LENGTH_SHORT).show();
                }
            }

        });

这是我的 RG 和 RB 的 xml 代码

<RadioGroup
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/RGroup">

                    <TableRow android:weightSum="1">
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Abdominal"
                        android:id="@+id/Abdominal"/>
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Arm"
                        android:id="@+id/Arm"/>
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Back"
                        android:id="@+id/Back" />
                        </TableRow>
                    <TableRow>
                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Chest"
                            android:id="@+id/Chest"/>
                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Leg"
                            android:id="@+id/Leg"/>
                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Shoulder"
                            android:id="@+id/Shoulder"/>
                    </TableRow>
                </RadioGroup>

EDITED 1: 回答 : 如果您不希望单选按钮可以一次被选择,所以不要使用Tablerow


由于 RadioGroup 内有 TableRow,它无法正常工作。由于 TableRow 位于所有 RadioButton 之间,因此它们不会组合在一起。

RadioButton 应该是 RadioGroup 的直接子级,否则分组不起作用。

只需像这样更改您的代码即可工作:

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/RGroup">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Abdominal"
                android:id="@+id/Abdominal"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Arm"
                android:id="@+id/Arm"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Back"
                android:id="@+id/Back" />                                        

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Chest"
                android:id="@+id/Chest"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Leg"
                android:id="@+id/Leg"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Shoulder"
                android:id="@+id/Shoulder"/>

        </RadioGroup>

希望这可以帮助。 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何设置RadioGroup中只有一个RadioButton可以同时选中 的相关文章

  • 在 Android 8 (Oreo) 的 Webview 中获取 Resources$NotFoundException

    我正在我的应用程序的 WebView 中加载网站的 URL 该网站获取用户的基本详细信息 我正在活动中加载 WebView 但是 当 Android 8 用户点击网站的下拉菜单时 应用程序会抛出以下错误 请注意 此 WebView 在 An
  • Android Studio 拒绝运行 main()

    重现步骤 在Android Studio中启动一个新项目 使用最新更新 创建一个新类并像往常一样添加 main 右键单击类以运行 main 作为测试 package test public class Test public static
  • Android - 如何创建可点击的列表视图?

    我想让列表视图中的所有列表项打开到一个新页面 因此每个列表视图项目都打开到一个我可以使用的新黑色页面 我根本不知道如何实现这个 我已经连续搜索了几个小时 但找不到解决方案的答案 如果有人能够展示和 或解释如何执行此操作而不是提供链接 我们将
  • 在 Android 中存储照片相关数据的最佳方式是什么? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我需要为我的应用程序存储一些照片相关信息 据我所知 您可以向图像内容提供商读取 写入 GPS 位置和图像描述 我还需要添加用于云同步的字段 标志
  • 为什么layout_width =“wrap_content”会占据屏幕的整个宽度?

    使用这个 xml
  • Eclipse 运行时 Dalvik 错误

    当我要运行任何程序时 会显示此对话框 在 Eclipse 中 这些错误显示在控制台中 2013 02 25 19 05 09 Dex Loader Unable to execute dex Target out of range 0000
  • 切换大小写错误。用于 Mac 上 Android 开发的 Eclipse IDE:将工作区合规性更改为 JRE 1.7

    当尝试在 android 项目中使用带有 switch string 的 Switch Case 时 我在 eclipse IDE 中收到错误消息 将工作区合规性更改为 JRE1 7 对于低于 1 7 的源级别 无法打开字符串类型的值 仅允
  • 如何更新不是我自己创建的 APK 的版本

    我是 Android 开发新手 我正在使用 x 平台开发工具 它为我构建并签署 APK 该工具有一个错误 它不会使用我指定的版本代码生成 APK 所有生成的 APK 都是版本 1 0 我想解压APK 编辑版本代码 然后辞职并重新打包 它最初
  • 单元测试定位服务

    我有一个位置跟踪服务 正在尝试对其进行单元测试 我正在尝试使用 locationManager addTestProvider 和 setTestProviderLocation 方法来实现此目的 但是 我似乎无法通过提供程序获取任何位置并
  • 将 match_parent 转换为“0dp”

    当我使用android layout height match parent or android layout width match parent 作为约束布局中子项的高度 宽度并构建 Gradle 文件 它会自动更改为android
  • Motorola Android 2.2 相机忽略 EXTRA_OUTPUT 参数

    我以编程方式打开相机来拍摄视频 我告诉相机使用如下代码将视频文件放置到指定位置 Intent intent new Intent MediaStore ACTION VIDEO CAPTURE File out new File sdcar
  • 在 Volley 中更新 UI 最有效的方法是什么

    最近我在 android 中使用 Volley 库 它工作得很好 但我想知道更新 UI 的最有效方法 我有一个包含所有 Volley 方法的 Utils 类 现在我传递了所有视图将作为参数更新 但我读到我可以在活动中实现侦听器 然后将它们作
  • 如何判断Android设备是否有触摸屏?

    我花了相当多的时间让我的 UI 仅支持键盘输入 但最终我不确定我是否可以相信 Android 设备都有触摸屏的假设 有没有办法判断Android设备是否有触摸屏 您应该研究现有设备并阅读Android 兼容性定义文档 http source
  • adb 无法启动守护进程,CreateProcess 失败,错误 2

    我无法运行adb root或任何 adb 命令 我收到以下错误 我设置ADB TRACE 1 C WINDOWS system32 gt adb root system core adb adb c main Handling comman
  • Android sqlite 缺少列

    我的 SQLite 数据库缺少一个我知道存在的列 我将无法从 Android 模拟器中提取数据库 因为如果不重写大量代码 就无法使用模拟器填充数据库 logcat 返回sqlite returned error code 1 msg tab
  • org.apache.http 软件包在 API 级别 23 中被删除。替代方案是什么?

    在更新到最新的 android API 级别 23 Marshmallow 后 通过 build gradle 添加以下更改后 所有 org apache http 类都不起作用 android compileSdkVersion 23 b
  • 从包中获取参数

    我正在尝试将参数从我的活动传递到片段 并且我正在使用以下代码 Override protected void onCreate Bundle savedInstanceState super onCreate savedInstanceSt
  • Android 中何时使用服务、何时不使用服务

    我从事Android开发已经不到两年了 我仍然对这个看似简单的问题感到困惑 何时应该实施一项服务 根据我的经验 有一些罕见的情况 但我对此表示怀疑 因为每部手机上都运行着很多这样的情况 我怀疑这只是一个糟糕的应用程序设计 这本质上是我的问题
  • 如何使用 Android 手机通话时播放声音?

    是否可以通过编程方式与电话进行交互 例如 您可以通过程序向呼叫者播放音频吗 Google 尚未公开任何允许我们向特定正在进行的调用提供数据的 API 不过 您可以控制通话 检查这篇文章 here http prasanta paul blo
  • SDK管理器不显示示例包选项

    我关注的是安卓安装示例的开发人员链接 http developer android com tools samples index html包裹 但是 当我展开Android平台的包列表时 没有 SDK示例 选项 复选框 可用 为什么 我已

随机推荐