如何向我以编程方式制作的广播组添加单选按钮?

时间:2015-08-12 14:01:46

标签: java android radio-button radio-group

我是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()方法

上出错

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我找到了解决办法!

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

这是我刚创建的课程:

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);
...
}

我不知道是否需要更多的方法或行为,这个课程就像我需要的那样。

感谢所有花时间在这个

的人