[英]How to check which radiogroup button is selected?
I have three radiobuttons in a radiogroup.我在一个单选组中有三个单选按钮。 How can I tell Java to do a different thing depending on which button is selected?
我怎样才能告诉 Java 根据选择的按钮做不同的事情? I have the group and all of the buttons declared:
我声明了组和所有按钮:
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);
I know I would say something like this:我知道我会这样说:
if (size.getCheckedRadioButtonId().equals(small){
} else{
}
But equals is not the correct syntax... How can I ask java which button is selected?但是 equals 不是正确的语法...我怎么能问 java 选择了哪个按钮?
Try:尝试:
if (size.getCheckedRadioButtonId() == small.getId()){
....
}
else if(size.getCheckedRadioButtonId() == medium.getId()){
....
}
Because getCheckedRadioButtonId()
returns an integer, you are trying to compare an integer with RadioButton object. You should compare the id of small
(which is R.id.RBS
) and getCheckedRadioButtonId()
:因为 getCheckedRadioButtonId
getCheckedRadioButtonId()
返回 integer,所以您正在尝试将 integer 与 RadioButton object 进行比较。您应该比较small
的 id(即R.id.RBS
)和 getCheckedRadioButtonId 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;
}
int selected = size.getCheckedRadioButtonId();
switch(selected){
case R.id.RBS:
break;
case R.id.RBM:
break;
case R.id.RBL:
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.