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

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

发布了76 篇原创文章 · 获赞 21 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/u010227042/article/details/104353302
今日推荐