如何将单选按钮添加到我以编程方式制作的单选组中? [英] How can I add radio buttons to a radio group I've made programmatically?

查看:46
本文介绍了如何将单选按钮添加到我以编程方式制作的单选组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android 完全陌生

I am completely new to Android

我尝试了任何我认为可行的方法,但每次出现错误时.这里有什么问题吗?

i tried whatever I thought may work but everytime I get error. What's wrong here?

ma​​in_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 行的广播组,但是当我尝试这样做时,它不起作用:

in fact I am trying to make a radio group with 3 columns and 2 rows but as I tried it doesn't work when I do it like this:

ma​​in_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() 方法上出错

the last solution I got is to make a radio group in Main.java programmatically but it makes an error on rg.addView() method

推荐答案

我找到了我的解决方案!

I've found my solution!

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

I created a new RadioGroup class which I can put my RadioButtons in a group without breaking my main.xml layout!

这是我刚刚创建的类:

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:

and create my RadioGroup like this:

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

我不知道是否需要更多的方法或行为,这个类正在按我的需要运行.

I don't know if more methods or behaviors is required or no, this class is acting as I needed.

感谢所有为此付出时间的人

thank everybody who took their time on this

这篇关于如何将单选按钮添加到我以编程方式制作的单选组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆