RadioGroup双列,可单选的效果

1.布局

使用一个radioGroup布局方式为vertical,再在里面放两个LinearLayout布局为horizontal,这样在布局上的双列效果就出来了


2.布局代码

使用这个布局后发现所有的raidobutton都可以被选择,变成了多选而且还取消不了


    android:id="@+id/rg_group"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:padding="10dp">

        android:id="@+id/top_advice_layout"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

            android:id="@+id/topic_item_option1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="提个建议"/>

            android:id="@+id/topic_item_option2"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="出错误啦"/>

        android:id="@+id/bottom_layout"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

            android:id="@+id/topic_item_option3"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="不好用"/>

            android:id="@+id/topic_item_option4"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:checked="true"

            android:text="其他"/>

3.activiy代码

为了解决多选的问题,写了一个更新radioGroup选择条目的方法,这样就可以只选择一个条目了,只用传入你需要被选择的item值即可



/**

* 更新group选择的item

* @param item

*/

private void upGroup(int item){

switch (item){

case 1:

mTopicItemOption1.setChecked(true);

mTopicItemOption2.setChecked(false);

mTopicItemOption3.setChecked(false);

mTopicItemOption4.setChecked(false);

break;

case 2:

mTopicItemOption1.setChecked(false);

mTopicItemOption2.setChecked(true);

mTopicItemOption3.setChecked(false);

mTopicItemOption4.setChecked(false);

break;

case 3:

mTopicItemOption1.setChecked(false);

mTopicItemOption2.setChecked(false);

mTopicItemOption3.setChecked(true);

mTopicItemOption4.setChecked(false);

break;

case 4:

mTopicItemOption1.setChecked(false);

mTopicItemOption2.setChecked(false);

mTopicItemOption3.setChecked(false);

mTopicItemOption4.setChecked(true);

break;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容