RadioButton设置默认选中后无法取消,可选中多个的问题

今天在做项目的时候出现一个低级错误,在多个RadioButton中,给其中RadioButton默认选中(android:checked="true"),运行后发现这个选中的RadioButton无法取消了,出现在一个RadioGroup中可以选择多个RadioButton的问题。这问题也许有点low,不过还是记录下给自己一个提醒吧。

这应该是基础不好的原因

问题:

在一个RadioGroup中可以选中两个RadioButton,且默认选中的那个一直为选中状态

 

代码:(简化后的代码,原来有很多属性)


    <RadioGroup 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        >
        <RadioButton 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:checked="true"
            android:text="1"/>
        <RadioButton 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="2"/>
        <RadioButton 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="3"/>
    </RadioGroup>

然后我就纳闷了好久,说好的单选呢???

接着就是百度,Google...找了好久也没找到答案(不知道是不是提问的方式不对),总之没找到结果

只好一个个属性删掉看看有没有问题(原来RadioButton有很多属性的),功夫不负有心人啊!!!最后还是找到原因了

上新的代码:


    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
 
        <RadioButton
            android:id="@+id/r1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="1" />
 
        <RadioButton
            android:id="@+id/r2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2" />
 
        <RadioButton
            android:id="@+id/r3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3" />
    </RadioGroup>


好像没什么区别啊,再仔细看看...比原先多了id这个属性
原来RadioButton是要设置id这个属性的,不然会导致RadioButton不互斥,后果很严重的啊

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PrinciplesMan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值