1

ラジオグループに3つのラジオボタンがあります。選択したボタンに応じて異なることを行うようにJavaに指示するにはどうすればよいですか?グループとすべてのボタンを宣言しました。

final RadioGroup size = (RadioGroup)findViewById(R.id.RGSize);
        final RadioButton small = (RadioButton)findViewById(R.id.RBS);
        final RadioButton medium = (RadioButton)findViewById(R.id.RBM);
        final RadioButton large = (RadioButton)findViewById(R.id.RBL);

私はこのようなことを言うだろうと知っています:

if (size.getCheckedRadioButtonId().equals(small){

} else{

}

しかし、equalsは正しい構文ではありません...どのボタンが選択されているかをJavaに尋ねるにはどうすればよいですか?

4

3 に答える 3

1

試す:

if (size.getCheckedRadioButtonId() == small.getId()){
 ....
}
else if(size.getCheckedRadioButtonId() == medium.getId()){
 ....
}
于 2012-04-04T00:49:07.690 に答える
1

整数を返すためgetCheckedRadioButtonId()、整数をRadioButtonオブジェクトと比較しようとしています。small(はR.id.RBS)とgetCheckedRadioButtonId():のIDを比較する必要があります

switch(size.getCheckedRadioButtonId()){
    case R.id.RBS: //your code goes here..
                    break;
    case R.id.RBM: //your code goes here..
                    break;
    case R.id.RBL: //your code goes here..
                    break;
}
于 2012-04-04T00:49:50.117 に答える
1
int selected = size.getCheckedRadioButtonId();

switch(selected){
case R.id.RBS:
   break;
case R.id.RBM:
   break;
case R.id.RBL:
   break;

}
于 2012-04-04T00:50:43.153 に答える