1

評価タイプの質問を作成しようとしていて、ラジオボタンに値を設定したい..しかし、チェックされたボタンの合計を計算することはできません..これは私のコードです..

public class MainActivity extends Activity {

public RadioButton r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
public RadioGroup rg1,rg2;
public TextView tv1;
public Button btn1;
public static int a,b,c,d,e,a1,b1,c1,d1,e1,total;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    r0 = (RadioButton)findViewById(R.id.radio0);
    r1 = (RadioButton)findViewById(R.id.radio1);
    r2 = (RadioButton)findViewById(R.id.radio2);
    r3 = (RadioButton)findViewById(R.id.radio3);
    r4 = (RadioButton)findViewById(R.id.radio4);
    r5 = (RadioButton)findViewById(R.id.radio5);
    r6 = (RadioButton)findViewById(R.id.radio6);
    r7 = (RadioButton)findViewById(R.id.radio7);
    r8 = (RadioButton)findViewById(R.id.radio8);
    r9 = (RadioButton)findViewById(R.id.radio9);


    btn1 = (Button)findViewById(R.id.button1);

    tv1 = (TextView)findViewById(R.id.textView7);

    rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
    rg2 = (RadioGroup)findViewById(R.id.radioGroup2);



    btn1.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {


    switch(rg1.getCheckedRadioButtonId()){

    case R.id.radio0:
        if (r0.isChecked()){
            a =1;



        }
            break;
    case R.id.radio1:
        if (r1.isChecked()){
            b = 2;


        }
            break;
    case R.id.radio2:
        if (r2.isChecked()){
            c = 3;


        }
            break;
    case R.id.radio3:
        if (r3.isChecked()){
            d = 4;

        }
            break;
    case R.id.radio4:
        if (r4.isChecked()){
            e = 5;


        }
            break;
    }

    //no. 2
    switch(rg2.getCheckedRadioButtonId()){

    case R.id.radio5:
        if (r5.isChecked()){
            a1=1;

        }
            break;
    case R.id.radio6:
        if (r6.isChecked()){
            b1 = 2;


        }
            break;
    case R.id.radio7:
        if (r7.isChecked()){
            c1 = 3;

        }
            break;
    case R.id.radio8:
        if (r8.isChecked()){
            d1 = 4;

        }
            break;
    case R.id.radio9:
        if (r9.isChecked()){
            e1 = 5;

        }
            break;
    }

// 私は 2 つのチェックされたラジオ ボタンを取得し、結果を出力したい.. pls は私を助けて

    total = rg1.getCheckedRadioButtonId() + rg2.getCheckedRadioButtonId();

    tv1.setText("result: "+total);



}


    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

4

1 に答える 1

0

これを見てください:

total = rg1.getCheckedRadioButtonId() + rg2.getCheckedRadioButtonId();

radio1、radio2 などの RadioButtonId を追加しようとしています。

これを行うには多くの方法があると思われますが、選択した数値を追加する合計変数を作成すると思います。

case R.id.radio5:
    if (r5.isChecked()){
        //a1=1;
        sum += 1;
    }

そして最後に

tv1.setText("result: "+ sum);

以前に追加し続けないように、毎回合計をリセットする必要があります。

于 2012-12-02T17:58:24.417 に答える