Android - RadioGroup中多个radiobutton同时被选中问题

问题描述:

动态创建radio button, 并将多个button添加到radio group中。但是实际运行时多个radiobutton会被同时选中:

代码如下:

复制代码
    mRadioGroup = findViewById(R.id.radioGroup);
    mDevButtons = new RadioButton[device_count];
 
    for(int i=0;i<device_count;i++) {
           mDevButtons[i] = new RadioButton(mContext);
           mDevButtons[i].setText(devices[i].getDeviceInfo());

           if(mCurrnetDeviceName != null && devices[i].getDeviceInfo().contains(mCurrnetDeviceName)) {
               mDevButtons[i].setChecked(true);
           }

           final UsbHidDevice device = devices[i];
           mDevButtons[i].setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   mCurrnetDeviceName = device.getDeviceName();
                   if (mCurrnetDeviceName != null) {
                       Toast.makeText(mContext, "Selected device: " + mCurrnetDeviceName, Toast.LENGTH_SHORT).show();
                   }
               }
           });
           mRadioGroup.addView(mDevButtons[i]);
    }

解决方法:

动态创建的radiobutton默认是没有button id的,需要主动分配button id。

mDevButtons[i].setId(View.generateViewId());

复制代码
    mRadioGroup = findViewById(R.id.radioGroup);
    mDevButtons = new RadioButton[device_count];
 
    for(int i=0;i<device_count;i++) {
           mDevButtons[i] = new RadioButton(mContext);
           mDevButtons[i].setText(devices[i].getDeviceInfo());
           mDevButtons[i].setId(View.generateViewId());

           if(mCurrnetDeviceName != null && devices[i].getDeviceInfo().contains(mCurrnetDeviceName)) {
               mDevButtons[i].setChecked(true);
           }

           final UsbHidDevice device = devices[i];
           mDevButtons[i].setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   mCurrnetDeviceName = device.getDeviceName();
                   if (mCurrnetDeviceName != null) {
                       Toast.makeText(mContext, "Selected device: " + mCurrnetDeviceName, Toast.LENGTH_SHORT).show();
                   }
               }
           });
           mRadioGroup.addView(mDevButtons[i]);
    }

解决原因:

radiogoup多个button之间互斥就是通过记录button id实现的。如果radio button没有button id,radio group无法得知具体是哪个button被选中,也就无法实现多个button互斥的功能。

相关推荐
没有bug.的程序员2 分钟前
黑客僵尸网络的降维打击:Spring Cloud Gateway 自定义限流剿杀 Sentinel 内存黑洞
java·网络·spring·gateway·sentinel
飘逸飘逸3 分钟前
Autojs进阶-插件更新记录
android·javascript
予枫的编程笔记4 分钟前
【面试专栏|Java并发编程】ConcurrentHashMap并发原理详解:JDK7 vs JDK8 核心对比
java·并发编程·hashmap·java面试·集合框架·jdk8·jdk7
程序员在线炒粉8元1份顺丰包邮送可乐6 分钟前
【Java 实现】用友 BIP V5 版本与飞书集成单点登录(飞书免密登录到用友 ERP)
java·开发语言·飞书·用友 bip
qq_411262429 分钟前
AP模式中修改下wifi名称就无法连接了,分析一下
java·前端·spring
东离与糖宝10 分钟前
Spring AI MCP Server正式落地,Java一键部署AI服务保姆级教程
java·人工智能
BUG创建者11 分钟前
uniapp 开发app时播放实时视频海康ws的流数据
前端·javascript·vue.js·uni-app·html·音视频
微露清风12 分钟前
系统性学习Linux-第八讲-进程间通信
java·linux·学习
Knight_AL13 分钟前
Java 中 Date 与 LocalDate 的区别
java·开发语言·数据库
Be for thing15 分钟前
Android 传感器硬件原理 + 功耗测试与异常定位实战(手表 / IoT / 手机通用)
android·学习·智能手机