1

我在一个单选组中有三个单选按钮。如何根据选择的按钮告诉 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) 和的 id getCheckedRadioButtonId()

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 回答