有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓如何通过java将RadioButton添加到RadioGroup?

It maybe a very simple question but i am beginner in Android please help with it.

我已经在XML中创建了两个RadioButton而没有RadioGroup,并且在MainActivity.java中创建了一个RadioGroup,如何在该RadioGroup中添加这两个RadioButton?代码如下

主要活动。xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context="com.avisingh.radiobuttontest.MainActivity">

<RadioButton
    安卓:id="@+id/male_button"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="10dp"
    安卓:text="Male"/>

<RadioButton
    安卓:id="@+id/female_button"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="50dp"
    安卓:text="Female"/>

</RelativeLayout>

主要活动。java

package com.avisingh.radiobuttontest;

import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.RadioButton;
import 安卓.widget.RadioGroup;
import 安卓.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final RadioButton maleBtn = (RadioButton)findViewById(R.id.male_button);
    final RadioButton femaleBtn = (RadioButton)findViewById(R.id.female_button);
    RadioGroup radioGroup = new RadioGroup(this);

    //radioGroup.add(maleBtn); // its getting error
    //radioGroup.add(femaleBtn); // its getting error
    //radioGroup.addView(maleBtn); // it is also getting error
    //radioGroup.addView(femaleBtn); // it is also getting error

    }
}

有没有什么方法可以像java一样同时添加RadioButton


共 (2) 个答案

  1. # 1 楼答案

    以下是你可以做到的

    RadioButton radioButton = new RadioButton(this);
        radioButton.setText("Test Radio");
        radioButton.setId(1);
        radioGrp.addView(radioButton);
    

    如果太多,试着把它放入循环

     for (int i = 0; i < radioItems.length; i++) {
            RadioButton radioButton = new RadioButton(this);
            radioButton.setText(radioItems[i]);
            radioButton.setId(i);
            radioGrp.addView(radioButton);
        }
    
  2. # 2 楼答案

    您可以在“activity_main”中添加“RadioGroup”。xml'

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.avisingh.radiobuttontest.MainActivity">
    <RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    <RadioButton
        android:id="@+id/male_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Male"/>
    
    <RadioButton
        android:id="@+id/female_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Female"/>
    </RadioGroup>