如何将单选按钮添加到以编程方式制作的单选按钮组中?

How can I add radio buttons to a radio group I've made programmatically?

我完全不熟悉 Android

我尝试了我认为可行的任何方法,但每次我都会出错。这里有什么问题?

main_activity.xml

<LinearLayout 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"
    android:padding="16dp"
    tools:context=".Main"
    android:orientation="vertical"
    android:id="@+id/mainLayout">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton1" />
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton2" />
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton3" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton4" />
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton5" />

        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton6" />
    </LinearLayout>
</LinearLayout>

Main.java

public class Main extends AppCompatActivity {

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

        RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
        RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
        RadioButton rb3 = (RadioButton) findViewById(R.id.radioButton3);
        RadioButton rb4 = (RadioButton) findViewById(R.id.radioButton4);
        RadioButton rb5 = (RadioButton) findViewById(R.id.radioButton5);
        RadioButton rb6 = (RadioButton) findViewById(R.id.radioButton6);

        RadioGroup rg = new RadioGroup(this);
        rg.addView(rb1);
        rg.addView(rb2);
        rg.addView(rb3);
        rg.addView(rb4);
        rg.addView(rb5);
        rg.addView(rb6);

    }

}

事实上,我正在尝试创建一个 3 列和 2 行的广播组,但是当我尝试这样做时它不起作用:

main_activity.xml

<LinearLayout 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"
    android:padding="16dp"
    tools:context=".Main"
    android:orientation="vertical"
    android:id="@+id/mainLayout">
  <RadioGroup
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton1" />
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton2" />
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton3" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton4" />
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton5" />
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New RadioButton"
            android:id="@+id/radioButton6" />
    </LinearLayout>
 </RadioGroup>
</LinearLayout>

我得到的最后一个解决方案是以编程方式在 Main.java 中创建一个无线电组,但它在 rg.addView() 方法

上出错

尝试在 xml 中创建 RadioGroup 并在 java 中获取对它的引用,然后使用该引用来完成您的工作。或者,如果您想保留 java RadioGroup,可以将 RadioGroup 添加到布局中。

我找到了我的解决方案!

我创建了一个新的 RadioGroup class,我可以将我的 RadioButtons 放在一个组中而不会破坏我的 main.xml 布局!

这是我刚刚创建的 class:

import android.view.View;
import android.widget.RadioButton;

/**
 * <p>
 *     CREATED:<br />&nbsp;&nbsp;&nbsp;&nbsp;
 *          15 Aug 2015
 * </p>
 * <p>
 *     This class is created to make a RadioGroup
 *     with no limitation of designing and layouting.
 *  <br />
 *  <strong>
 *      The first method you have to call is
 *      <em>createRadioGroup ()</em> to
 *      initialize your RadioGroup.
 *  </strong>
 *  <br />
 *     Feel free to browse in the class
 *     and making change in it as you need.
 * </p>
 * @author ShahinSorkh
 * @version 1
 */
public class RadioGroup2 {

// Global variables
private RadioButton rb [];
private int firstCheckedItemId;
public static int countRadioButtons;

/**
 * <p>
 *     will initialize your RadioGroup
 * </p>
 * <p>
 *     <strong>an example:</strong>
 *     <p>
 *         RadioGroup2 rg = new RadioGroup2;<br />
 *         RadioButton rb [] = new RadioButton[3];<br />
 *         rb[0] = (RadioButton)findViewById(R.id.radio1);<br />
 *         rb[1] = (RadioButton)findViewById(R.id.radio2);<br />
 *         rb[2] = (RadioButton)findViewById(R.id.radio3);<br />
 *         rg.createRadioGroup(rb);
 *     </p>
 * </p>
 * @param radioButtons an Array of RadioButtons
 */
public void createRadioGroup(RadioButton [] radioButtons){
    rb = radioButtons;
    firstCheckedItemId = getCheckedItemId();
    countRadioButtons = rb.length;
    onCheck();
}

private void onCheck(){
    for (RadioButton aRb : rb) {
        aRb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View radio) {
                final RadioButton myRb = (RadioButton) radio;
                for (RadioButton aRb : rb) {
                    if (aRb.getId() == myRb.getId()) {
                        aRb.setChecked(true);
                    } else aRb.setChecked(false);
                }
            }
        });
    }
}

/**
 * will count RadioButtons in your RadioGroup
 * @return the count of RadioButtons in RadioGroup
 */
public int getRadioButtonsCount(){
    return countRadioButtons;
}

/**
 * <p>
 *     will return the checked item <strong>ID</strong>
 *     <em>you can use in findViewById() for example</em>
 * </p>
 * <p>
 *     will return <em>-1</em> if no RadioButton has checked<br />
 *     better to check if any button has checked
 * </p>
 * @return int checkedRadioButtonId
 */
public int getCheckedItemId (){
    int id = -1;
    for (RadioButton aRb : rb) {
        if(aRb.isChecked())
            id = aRb.getId();
    }
    return id;
}

/**
 * <p>
 *     will return the checked <strong>RadioButton</strong>
 * </p>
 * <p>
 *     will return <em>null</em> if no RadioButton has checked<br />
 *     better to check if any button has checked
 * </p>
 * @return checked RadioButton directly
 */
public RadioButton getCheckedItem (){
    RadioButton RB = null;
    for (RadioButton aRb : rb) {
        if(aRb.isChecked())
            RB = aRb;
    }
    return RB;
}

/**
 * will reset all RadioButtons checked state
 * to default, all RadioButtons will unCheck
 * and the firstChecked RadioButton will check
 * at end
 */
public void resetButtons(){
    RadioButton iRb = getCheckedItem();
    if(iRb != null)
        iRb.setChecked(false);
    if(firstCheckedItemId != -1)
        getChildById(firstCheckedItemId).setChecked(true);
}

private RadioButton getChildById(int id){
    RadioButton iRb = null;
    for (RadioButton aRb : rb ) {
        if(aRb.getId() == id) {
            iRb = aRb;
            break;
        }
    }
    return iRb;
}

/**
 * <p>
 *     will return <strong>RadioButton</strong> at given index in the Array
 * </p>
 * @param position index of RadioButton in Array
 * @return RadioButton[i]
 */
public RadioButton getChildAt (int position){
    return rb[position];
}
}

并像这样创建我的 RadioGroup:

RadioGroup2 rg = new RadioGroup2 ();
RadioButton [] rb = new RadioButton [3]();

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

rb[0] = (RadioButton) findViewById(R.id.rb1);
rb[1] = (RadioButton) findViewById(R.id.rb2);
rb[2] = (RadioButton) findViewById(R.id.rb3);

rg.createRadioGroup(rb);
...
}

我不知道是否需要更多的方法或行为,这个class正在按我的需要行事。

感谢所有花时间阅读此内容的人