0

我想做的是我头上有 3 个单选按钮说是,不,不适用

后跟 3 行中的 2 个单选按钮

我想要的是如果在 3 Rows 标题 YES 中选中所有 YES

如果在 3 行中的任何一个中检查 NO,则检查标题 No

我怎样才能做到这一点

我为此制作了 3 个无线电组

请通过图片参考 在此处输入图像描述

我试过的是

对于标题行

                vctOptionPoint=new Vector<String>();
                vctAnswerID=new Vector<String>();
                int iOpCount=3;
                RadioGroup rg=new RadioGroup(this);
                rg.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                rg.setId(idRadioGroup);
                rg.setOrientation(android.widget.RadioGroup.HORIZONTAL);
                rg.setPadding(5, 0, 0, 0);
                for(int j=0;j<iOpCount;j++){
                    String sOption="";
                    if(j==0){
                        sOption="Yes";
                        vctOptionPoint.addElement(sQuestionPoints);
                    }
                    else if(j==1){
                        sOption="No";
                        vctOptionPoint.addElement("0");
                    }
                    if(j==2){
                        sOption="NA";
                        vctOptionPoint.addElement("0");
                    }
                    vctAnswerID.addElement(String.valueOf(j));

                    RadioButton rb=new RadioButton(this);
                    rb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                    rb.setText(sOption);
                    if(sAnswerText.toString().equals(sOption)){
                        rb.setChecked(true);
                    }
                    rb.setId(idRadioButton);
                    idRadioButton++;                            
                    rg.addView(rb);
                }
                rg.setOnCheckedChangeListener(rb_onCheckedChange);
                ll.addView(rg);

第 2 行

vctOptionPoint2=new Vector<String>();
            vctAnswerID2=new Vector<String>();
            int iOpCount=2;
            RadioGroup rg1=new RadioGroup(this);
            rg1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            rg1.setId(idRadioGroup);
            rg1.setOrientation(android.widget.RadioGroup.HORIZONTAL);
            rg1.setPadding(5, 0, 0, 0);
            for(int j=0;j<iOpCount;j++){
                String sOption="";
                if(j==0){
                    sOption="Yes";
                    vctOptionPoint2.addElement(sQuestionPoints);
                }
                else if(j==1){
                    sOption="No";
                    vctOptionPoint2.addElement("0");
                }
                vctAnswerID2.addElement(String.valueOf(j));

                RadioButton rb1=new RadioButton(this);
                rb1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                rb1.setText(sOption);
                rb1.setChecked(false);
                if(sAnswerText.toString().equals(sOption)){
                    //rb1.setChecked(true);
                }
                rb1.setId(idRadioButton2);
                idRadioButton2++;                           
                rg1.addView(rb1);
            }
            rg1.setOnCheckedChangeListener(rb_onCheckedChange_New);

相同的前 3 和 4 排

检查更改方法

OnCheckedChangeListener rb_onCheckedChange_New=new OnCheckedChangeListener(){
        public void onCheckedChanged(RadioGroup rg,int arg1){
            idRadioButton=4;
            idRadioButton2=15;
            idRadioButton1=13;
            idRadioButton11=14;


            int iRGChildCount=rg.getChildCount();
            for(int yy=0;yy<iRGChildCount;yy++){
                RadioButton rb=(RadioButton)rg.findViewById(idRadioButton);
                RadioButton rb1=(RadioButton)rg.findViewById(idRadioButton2);
                RadioButton rb2=(RadioButton)rg.findViewById(idRadioButton1);
                RadioButton rb3=(RadioButton)rg.findViewById(idRadioButton11);

                if((rb1!=null)){
                    if(rb1.isChecked() && rb2.isChecked() && rb3.isChecked()){
                        //int iQusPoints=Integer.parseInt((String)vctOptionPoint2.elementAt(idRadioButton-4));
                        String sOption1=rb1.getText().toString();
                        String sOption2=rb2.getText().toString();
                        String sOption3=rb3.getText().toString();

                        if(sOption1.equals("No") || sOption2.equals("No") || sOption3.equals("No") ){
                            tvSecCount1.setText("Points : 0\nScored : 0");
                        }
                        else if(sOption1.equals("Yes") || sOption2.equals("No") || sOption3.equals("No") ){
                            int iQusPoints=0;
                            tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);

                        }
                        else if(sOption1.equals("No") || sOption2.equals("Yes") || sOption3.equals("No") ){
                            int iQusPoints=0;
                            tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);

                        }
                        else if(sOption1.equals("No") || sOption2.equals("No") || sOption3.equals("Yes") ){
                            int iQusPoints=0;
                            tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);

                        }
                        else if(sOption1.equals("Yes") || sOption2.equals("Yes") || sOption3.equals("No") ){
                            int iQusPoints=0;
                            tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);

                        }
                        else if(sOption1.equals("No") || sOption2.equals("Yes") || sOption3.equals("Yes") ){
                            int iQusPoints=0;
                            tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);
                        }
                        else{
                            int iQusPoints=2;
                            tvSecCount1.setText("Points : "+sQuestionPoints+"\nScored : "+iQusPoints);
                            if(sOption1.equals("Yes") && sOption2.equals("Yes") && sOption3.equals("Yes") ){
                                //fnNext();
                            }
                        }
                    }
                }
                idRadioButton++;
            }
        }
    };
4

1 回答 1

1

res/values/strings.xml(请始终使用 strings.xml 文件)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="yes">Yes</string>
    <string name="no">No</string>
    <string name="na">NA</string>
</resources>

res/layout/activity_main.xml (以任何你想要的方式改变布局,我让它变得非常简单只是为了测试目的)

<RadioGroup
    android:id="@+id/radioHeader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="14dp" >

    <RadioButton
        android:id="@+id/radioHeaderYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/yes" />

    <RadioButton
        android:id="@+id/radioHeaderNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no" />

    <RadioButton
        android:id="@+id/radioHeaderNA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/na" />
</RadioGroup>

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio1Yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/yes" />

    <RadioButton
        android:id="@+id/radio1No"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no" />
</RadioGroup>

<RadioGroup
    android:id="@+id/radioGroup2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio2Yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/yes" />

    <RadioButton
        android:id="@+id/radio2No"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no" />
</RadioGroup>

<RadioGroup
    android:id="@+id/radioGroup3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio3Yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/yes" />

    <RadioButton
        android:id="@+id/radio3No"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no" />
</RadioGroup>

MainActivity.java(看看我需要多少 Java 代码……)

public class MainActivity extends Activity {

Boolean[] radioGroups;
RadioGroup radioHeader, radioGroup1, radioGroup2, radioGroup3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    radioGroups = new Boolean[] {true, true, true};
    initViews();
}

private void initViews() {
    radioHeader = (RadioGroup) this.findViewById(R.id.radioHeader);
    radioGroup1 = (RadioGroup) this.findViewById(R.id.radioGroup1);
    radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            radioGroups[0] = checkedId == R.id.radio1Yes;
            refreshHeader();
        }
    });
    radioGroup2 = (RadioGroup) this.findViewById(R.id.radioGroup2);
    radioGroup2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            radioGroups[1] = checkedId == R.id.radio2Yes;
            refreshHeader();
        }
    });
    radioGroup3 = (RadioGroup) this.findViewById(R.id.radioGroup3);
    radioGroup3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            radioGroups[2] = checkedId == R.id.radio3Yes;
            refreshHeader();
        }
    });
}

private void refreshHeader() {
    radioHeader.check(radioGroups[0] & radioGroups[1] & radioGroups[2] ? R.id.radioHeaderYes : R.id.radioHeaderNo);
}
于 2013-02-01T12:33:20.520 回答