android 的radiogroup显示多个radiobutton的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 的radiogroup显示多个radiobutton的问题相关的知识,希望对你有一定的参考价值。

就是我要是在radiogroup中设置成横向布局,我的那个7个radiobutton就会有一些超出屏幕,我用网上的方法就是在radiogroup中设置linerlayout,比如 <RadioGroup
android:id="@+id/RG"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/b1"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="1" />

<RadioButton
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="2" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="3" />

<RadioButton
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="4" />
</LinearLayout>
</RadioGroup>

这样的话就没法单选了,就是所有的radiobutton都可以同时选。请问这个怎么解决?

参考技术A 将<LinearLayout></LinearLayout>去掉就OK了 是你的布局又问题 如下:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RadioGroup
android:id="@+id/RG"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/b1"
android:orientation="vertical" >

<RadioButton
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="1" />

<RadioButton
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="2" />

<RadioButton
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="3" />

<RadioButton
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="4" />
</RadioGroup>

</LinearLayout>

希望对LZ有帮助!追问

谢谢您的回答,但是您没明白我的意思,现在我的radiogroup只是两个按钮,如果我要是弄10个按钮的话就会有很多超出屏幕显示了,我想让这10个钮都显示在屏幕里,网上说是在radiogroup里加布局,可是我试完是能都显示在屏幕里,但是没有单选的效果了

追答

我今天上班时间 用了各种布局测试 得出结果:
如果在radioGroup中添加布局文件,会影响radioButton的互斥性,而且radioGroup没有标记属性 ,所以不可行。
然后在各个布局中发现radioGroup的属性只能是子项水平或者竖直,不能进行换行。那么要添加多个radioButton并且让他们互斥,只有调节radioButton的大小,尽可能多的添加子项了。
这是我个人测试的答案,如果有人能够实现,还请指正!

本回答被提问者采纳
参考技术B 可以自定义RadioGroup 参考技术C LZ建议你设置成纵向布局追问

但是纵向布局也不够地儿啊,我加了10个radiobutton

追答

可以把RadioGroup放在ScrollView里,这样可以上下滑动,你添加多少个radiobutton都没关系,

追问

谢谢啊,这到也算个办法。我现在把那堆东西都放一个gridview里了,感觉还行

参考技术D 不要加布局,你的RadioGroup只是一个控件..控件里面加布局..那么一选就选中一个布局里面的东西了..追问

那请问如何能让10个按钮都显示在屏幕里呢?

追答

为什么不做个长按监听呢..
一定要做10个按钮在桌面..

追问

刚学这玩意,就会点基础的现在,囧

关于 RadioGroup 和 RecyclerView 的问题

【中文标题】关于 RadioGroup 和 RecyclerView 的问题【英文标题】:Question about RadioGroup and RecyclerView 【发布时间】:2019-04-05 11:06:07 【问题描述】:

我具有将用户手机中的铃声列表显示为 RecyclerView 内的 RadioGroup 的功能 RadioGroup 的大小随着用户手机的大小而动态增加 一切正常,除了当用户选择一个铃声,点击保存然后再次返回,它不显示已选择的铃声 如何从动态 RadioGroup 中获取单选按钮的索引/ID?

【问题讨论】:

您能否添加一个最小、完整和可验证的示例,请参阅***.com/help/mcve ?否则很难判断出了什么问题...... How to get the selected index of a RadioGroup in Android的可能重复 【参考方案1】:

在像 oncheckedchangelistener 这样的单选按钮监听器上获取它的 id 并保存它。之后,当屏幕的 oncreate 获取该 id 并设置为选中状态。

模型类中的另一件事是设置一个布尔变量,用于定义是否选中单选按钮。

【讨论】:

以上是关于android 的radiogroup显示多个radiobutton的问题的主要内容,如果未能解决你的问题,请参考以下文章

Android 谁有动态添加多个RadioGroup 并获取所有RadioGroup被选中的值的方法?

可以在 RadioGroup android 中选择多个 RadioButton

Android RadioGroup和RadioButton详解

android的radiogroup为啥选择两个

Android如何动态生成Radio和RadioGroup

RadioGroup分频器没有显示