如何检测哪个radiogroup按钮被选中了呢?

如何检测哪个radiogroup按钮被选中了呢?

问题描述:

在radiogroup中有三个radiobuttons,我使用Java如何实现不同按钮被选中会发生不同的动作?下面是关于radiogroup和radiobutton的代码:

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实现按钮被选中后发生动作的功能?

int selected = size.getCheckedRadioButtonId();

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

}

使用这段代码可以实现:

 if (size.getCheckedRadioButtonId() == small.getId()){
     ....
    }
    else if(size.getCheckedRadioButtonId() == medium.getId()){
     ....
    }

getCheckedRadioButtonId