【问题标题】:How to add radiobuttons to radio group in 3*10 grid layout dynamically如何在 3*10 网格布局中动态地将单选按钮添加到单选组
【发布时间】:2016-12-21 12:10:57
【问题描述】:

当我启动代码时,我正在尝试将单选按钮添加到具有表格布局的单选组中,但出现了一些错误。这是我的代码

    RadioButton[] boxes = new RadioButton[30];
    RadioGroup rg = (RadioGroup) findViewById(R.id.radiogroup);

    for (int i = 0; i < 30; i++) {
        boxes[i] = (RadioButton) findViewById(idArray[i]);
    }

    for (int i = 0; i < 30; i++) {
        rg.addView(boxes[i]);
    }

xml代码在这里`

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="11-12(T1)"
                android:id="@+id/radioButton0"
                android:layout_column="9"
                android:checked="false" />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="11-12(T2)"
                android:id="@+id/radioButton10"
                android:checked="false"
                android:layout_marginLeft="40dp" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="11-12(T3)"
                android:id="@+id/radioButton20"
                android:layout_column="11"
                android:checked="false"
                android:layout_marginLeft="40dp" />
        </TableRow>
     </TableLayout>
</RadioGroup>

我没有发布整个 xml 代码,因为有 10 个表格行,每个表格行有 3 个单选按钮,所以我在 3*10 网格中有 30 个单选按钮。在 java 代码中我得到类似的错误“指定的孩子已经有一个父母。你必须先在孩子的父母上调用 removeView()。”

【问题讨论】:

    标签: android radio-button radio-group


    【解决方案1】:

    由于 RadioGroup 中的 TableLayout,它无法正常工作。由于它们之间的 TableLayout,所有 RadioButton 都没有组合在一起。

    RadioButton 应该是 RadioGroup 的直接子级,否则分组不起作用。

    【讨论】:

      【解决方案2】:

      如日志中所述,单选按钮已经有一个父级。 您将它们添加到 xml 中的 Table Row 中,这意味着您不能将它们添加到另一个父级(您的单选组)。 此外,尝试在您的编码中重用您已经编写的代码。它将更具可读性,而不是在 xml 中使用相同的行。例如,考虑使用“include”标签。

      也许会有所帮助。因为我正是这样做的——表格布局中的单选按钮。这是一个自定义布局。只需使用它来添加您想要的任何单选按钮:

      视图代码在链接中:https://github.com/Gavras/MultiLineRadioGroup/blob/master/app/src/main/java/com/whygraphics/multilineradiogroup/MultiLineRadioGroup.java

      确保也添加这些:

      值/attrs.xml 文件:

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
          <declare-styleable name="multi_line_radio_group">
              <attr name="max_in_row" format="integer" />
              <attr name="radio_buttons" format="reference" />
              <attr name="default_button" format="string" />
          </declare-styleable>
      </resources>
      

      R.layout.table_layout:

      <?xml version="1.0" encoding="utf-8"?>
      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/table_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:stretchColumns="*" />
      

      R.layout.table_row:

      <?xml version="1.0" encoding="utf-8"?>
      <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/table_row"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
      

      R.layout.radio_button:(您可以在此处更改文字大小)

      <?xml version="1.0" encoding="utf-8"?>
      <RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/radio_button"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:textSize="@dimen/radio_button_text_size" />
      

      使用 xml 中的此布局的示例:

      <?xml version="1.0" encoding="utf-8"?>
      <[package].MultiLineRadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:multi_line_radio_group="http://schemas.android.com/apk/res-auto"
          android:id="@+id/multi_line_radio_group"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          multi_line_radio_group:max_in_row="3"
          multi_line_radio_group:radio_buttons="@array/radio_buttons" />
      

      您可以从 xml 添加带有所需字符串的 res/values/arrays.xml 或者使用 addButtons() 方法从代码中添加。

      【讨论】:

        【解决方案3】:

        将单选按钮添加到 RadioGroup,然后将 RadioGroup 添加到布局。最后用这个来展示!

        for (int i = 0; i < 10; i++) {
        
                    for (int j = 0; j < 3; j++) {
                        radioGroup[j].removeView(radioButton[i]); //now the RadioButtons are in the RadioGroup
                    }
                    rowLayout.removeView(radioGroup[j]); // now the RadiouGroup are in the row
                }
        

        【讨论】:

          猜你喜欢
          • 2012-05-20
          • 1970-01-01
          • 2011-01-04
          • 1970-01-01
          • 2011-12-25
          • 1970-01-01
          • 2023-03-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多