如何检查选择了哪个radiogroup按钮?

时间:2012-04-04 00:39:47

标签: java android radio-button radio-group

我在一个放射组中有三个radiobuttons。如何根据选择的按钮告诉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选择了哪个按钮?

3 个答案:

答案 0 :(得分:1)

尝试:

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

答案 1 :(得分:1)

因为getCheckedRadioButtonId()返回一个整数,所以你试图将一个整数与RadioButton对象进行比较。您应该比较smallR.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;
}

答案 2 :(得分:1)

int selected = size.getCheckedRadioButtonId();

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

}
相关问题