从radiobutton和radiogroup android获取价值

时间:2015-05-11 06:47:30

标签: android radio-button eclipse-adt android-radiogroup android-radiobutton

//这是我的代码,但结果仍为0 ...任何人都可以帮忙修复我的代码? thx之前:)

RadioGroup rg_ringkas, rg_rapi, rg_resik, rg_rawat, rg_rajin;
    RadioButton r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20,r21,r22,r23,r24,r25;
    TextView textview_total;
    public static int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,total;
    int sum = 0;

    r1 = (RadioButton) findViewById(R.id.radioButton1);
    r2 = (RadioButton) findViewById(R.id.radioButton1);
    r3 = (RadioButton) findViewById(R.id.radioButton1);
    r4 = (RadioButton) findViewById(R.id.radioButton1);
    r5 = (RadioButton) findViewById(R.id.radioButton1);
    r6 = (RadioButton) findViewById(R.id.radioButton1);
    r7 = (RadioButton) findViewById(R.id.radioButton1);
    r8 = (RadioButton) findViewById(R.id.radioButton1);
    r9 = (RadioButton) findViewById(R.id.radioButton1);
    r10 = (RadioButton) findViewById(R.id.radioButton1);
    r11 = (RadioButton) findViewById(R.id.radioButton1);
    r12 = (RadioButton) findViewById(R.id.radioButton1);
    r13 = (RadioButton) findViewById(R.id.radioButton1);
    r14 = (RadioButton) findViewById(R.id.radioButton1);
    r15 = (RadioButton) findViewById(R.id.radioButton1);
    r16 = (RadioButton) findViewById(R.id.radioButton1);
    r17 = (RadioButton) findViewById(R.id.radioButton1);
    r18 = (RadioButton) findViewById(R.id.radioButton1);
    r19 = (RadioButton) findViewById(R.id.radioButton1);
    r20 = (RadioButton) findViewById(R.id.radioButton1);
    r21 = (RadioButton) findViewById(R.id.radioButton1);
    r22 = (RadioButton) findViewById(R.id.radioButton1);
    r23 = (RadioButton) findViewById(R.id.radioButton1);
    r24 = (RadioButton) findViewById(R.id.radioButton1);
    r25 = (RadioButton) findViewById(R.id.radioButton1);

    rg_ringkas = (RadioGroup) findViewById(R.id.radioGroupRingkas);
    rg_rapi = (RadioGroup) findViewById(R.id.radioGroupRapi);
    rg_resik = (RadioGroup) findViewById(R.id.radioGroupResik);
    rg_rawat = (RadioGroup) findViewById(R.id.radioGroupRawat);
    rg_rajin = (RadioGroup) findViewById(R.id.radioGroupRajin);

    textview_total = (TextView) findViewById(R.id.textview_total);
    //rg_ringkas
    switch (rg_ringkas.getCheckedRadioButtonId()) {
    case R.id.radioButton1:
        if(r1.isChecked()){
            //a = 1;
            sum += 1;
        }
        break;

    case R.id.radioButton2:
        if(r2.isChecked()){
            //b = 2;
            sum += 2;
        }
        break;

    case R.id.radioButton3:
        if(r3.isChecked()){
            //c = 3;
            sum += 3;
        }
        break;

    case R.id.radioButton4:
        if(r4.isChecked()){
            //d = 4;
            sum += 4;
        }
        break;

    case R.id.radioButton5:
        if(r5.isChecked()){
            //e = 5;
            sum += 5;
        }
        break;

    default:
        break;
    }
    //rg_rapi
    switch (rg_rapi.getCheckedRadioButtonId()) {
    case R.id.radioButton6:
        if(r6.isChecked()){
            //f = 1;
            sum += 1;
        }
        break;

    case R.id.radioButton7:
        if(r7.isChecked()){
            //g = 2;
            sum += 2;
        }
        break;

    case R.id.radioButton8:
        if(r8.isChecked()){
            //h = 3;
            sum += 3;
        }
        break;

    case R.id.radioButton9:
        if(r9.isChecked()){
            //i = 4;
            sum += 4;
        }
        break;

    case R.id.radioButton10:
        if(r10.isChecked()){
            //j = 5;
            sum += 5;
        }
        break;

    default:
        break;
    }
    //rg_resik
    switch (rg_resik.getCheckedRadioButtonId()) {
    case R.id.radioButton11:
        if(r11.isChecked()){
            //k = 1;
            sum += 1;
        }
        break;

    case R.id.radioButton12:
        if(r12.isChecked()){
            //l = 2;
            sum += 2;
        }
        break;

    case R.id.radioButton13:
        if(r13.isChecked()){
            //m = 3;
            sum += 3;
        }
        break;

    case R.id.radioButton14:
        if(r14.isChecked()){
            //n = 4;
            sum += 4;
        }
        break;

    case R.id.radioButton15:
        if(r15.isChecked()){
            //o = 5;
            sum += 5;
        }
        break;

    default:
        break;
    }
    //rg_rawat
    switch (rg_rawat.getCheckedRadioButtonId()) {
    case R.id.radioButton16:
        if(r16.isChecked()){
            //p = 1;
            sum += 1;
        }
        break;

    case R.id.radioButton17:
        if(r17.isChecked()){
            //q = 2;
            sum += 2;
        }
        break;

    case R.id.radioButton18:
        if(r18.isChecked()){
            //r = 3;
            sum += 3;
        }
        break;

    case R.id.radioButton19:
        if(r19.isChecked()){
            //s = 4;
            sum += 4;
        }
        break;

    case R.id.radioButton20:
        if(r20.isChecked()){
            //t = 5;
            sum += 5;
        }
        break;

    default:
        break;
    }
    //rg_rajin
    switch (rg_rajin.getCheckedRadioButtonId()) {
    case R.id.radioButton21:
        if(r21.isChecked()){
            //u = 1;
            sum += 1;
        }
        break;

    case R.id.radioButton22:
        if(r22.isChecked()){
            //v = 2;
            sum += 2;
        }
        break;

    case R.id.radioButton23:
        if(r23.isChecked()){
            //w = 3;
            sum += 3;
        }
        break;

    case R.id.radioButton24:
        if(r24.isChecked()){
            //x = 4;
            sum += 4;
        }
        break;

    case R.id.radioButton25:
        if(r25.isChecked()){
            //y = 5;
            sum += 5;
        }
        break;

    default:
        break;
    }

    //total = rg_ringkas.getCheckedRadioButtonId() + rg_rapi.getCheckedRadioButtonId() + rg_resik.getCheckedRadioButtonId() + rg_rawat.getCheckedRadioButtonId() + rg_rajin.getCheckedRadioButtonId();
    textview_total.setText(""+sum);

2 个答案:

答案 0 :(得分:1)

为您的所有RadioGroup

做这样的事情
 rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub

                    int childCount=group.getChildCount();
                    for (int i=0;i<childCount;i++){

                            RadioButton r_btn = (RadioButton) group.getChildAt(i);
                            if (r_btn.getId() == checkedId) {
                               // do your stuf here
                                 sum1=i+1; // since position is from 0.
                            }

                    }

                }
            });

rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub

                    int childCount=group.getChildCount();
                    for (int i=0;i<childCount;i++){

                            RadioButton r_btn = (RadioButton) group.getChildAt(i);
                            if (r_btn.getId() == checkedId) {
                               // do your stuf here
                                 sum2=i+1; // since position is from 0.
                            }

                    }

                }
            });


 hitung.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

             total.setText(""+sum1+sum2);

        }
    });

答案 1 :(得分:0)

您可以引用无线电组,并使用getCheckedRadioButtonId ()获取已选中的单选按钮ID。看看这里

RadioGroup radioGroup = findViewById(R.id.radio_group);

然后当你需要获得所选的无线电选项时。

int id = radioGroup.getCheckedRadioButtonId();
if (id == -1){
    //no item selected
}
else{
    if (id == R.id.radio_button1){
        //Do something with the button
    }
}

对于单选按钮,首先更改您的按钮ID ..您使用相同的ID(radioButton1)为您所有按钮的