【问题标题】:RadioGroup in expandablelistview可展开列表视图中的 RadioGroup
【发布时间】:2016-02-06 06:56:42
【问题描述】:

我正在尝试用可扩展的列表视图做这样的事情:

PARENT 1:
    - child 1 ( Radio Button)
    - child 2 ( Radio Button)
    - child 3 (Radio Button)
    - child 4 (Checkbox)
    - child 5 (Checkbox)
PARENT 2 ...

但是:child 1,child2,child3 应该像一个无线电组。因此,例如,如果我选中 child2,child1 和 child3 应该被取消选中。

我已经设法做到这一点:一些孩子有单选按钮,一些有复选框。但单选按钮不相互连接,也不像单选组。

谁能帮我解决这个问题?

我的 XML 代码:

    <?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:focusable="false"
    >

        <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/checkbox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:clickable="false"
            android:layout_column="0"
            android:layout_row="0" />


        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            android:gravity="center_vertical"
            android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
            android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
            android:paddingRight="?android:attr/listPreferredItemPaddingRight"
            android:minHeight="?android:attr/listPreferredItemHeightSmall"
            android:focusable="false"
            android:layout_column="2"
            android:layout_row="0" />



    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text2"
        android:layout_row="1"
        android:focusable="false"
        android:layout_column="2" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioButton"
        android:layout_row="0"
        android:layout_column="0"
        android:focusable="false"
        android:clickable="false"/>



</GridLayout>

【问题讨论】:

  • 如果您的单选按钮未分组,则使用单选按钮没有意义。也许,如果您能进一步解释您想要达到的目标,那么我们可能会提供进一步的建议。
  • 好的。我的应用程序是服务计算。并且消费者选择一些服务(他/她应该检查复选框)并且应用程序计算整个总和。但是有些服务是可选的——我可以选择这个或另一个。这就是为什么我需要像广播组一样的单选按钮。

标签: android checkbox radio-button expandablelistview


【解决方案1】:

您必须将它们放入广播组。看http://developer.android.com/guide/topics/ui/controls/radiobutton.html

以下是指南中显示的示例,该示例适用于您为每个父母执行的任务。 (使用您需要的属性对其进行个性化)

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <RadioButton android:id="@+id/child1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <RadioButton android:id="@+id/child2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <RadioButton android:id="@+id/child3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RadioGroup>

<CheckBox android:id="@+id/child4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<CheckBox android:id="@+id/child5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

【讨论】:

  • 问题是,如果我这样做,每个孩子都会连续有 3 个单选按钮;(
  • 只需将 RadioGroup 的方向设置为水平
  • 我的 xml 文件是这样设置的,我只有一个复选框、一个单选框和 2 个文本视图。我将它用于每一行。在应该是复选框的行中,我使单选按钮不可见,反之亦然
  • 对不起,我不明白你的问题。我的 xml 代码用于静态定义,如果您的应用程序具有可变数量的此模式来定义您必须以编程方式创建布局。编写一个创建此模式的函数并使用相应的参数调用它 n 次
  • 我还是不明白你的问题。你会用 3 个单选按钮代替你的 吗?然后拿走我的 并替换它
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-15
  • 1970-01-01
  • 2011-10-29
相关资源
最近更新 更多