-1

//這裏是我的代碼,但結果仍然是0 ...任何人都可以幫助修復我的代碼?前:)從radiobutton和radiogroup獲取價值android

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); 
+1

爲什麼不你爲每個RadioGroup使用setOnCheckedChangeListener嗎? – Rustam

+1

首先改變你的按鈕ID ..你使用相同的ID爲你所有的按鈕的 findViewById(R.id.radioButton1); –

回答

1

你用相同的ID(radioButton1)做這樣的滿足您的所有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); 

     } 
    }); 
0

你可以有收音機組的參考和使用getCheckedRadioButtonId() THX得到檢查單選按鈕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。對於所有您按鈕的