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不互斥,后果很严重的啊


以上有错误之处欢迎指出,谢谢!!!




评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值