【问题标题】:Android: Why Radio Button in Radio Group always returns false for any item selected?Android:为什么 Radio Group 中的 Radio Button 总是为任何选定的项目返回 false?
【发布时间】:2014-06-13 20:50:19
【问题描述】:
我在一个单选组中有两个单选按钮,当我选择任何单选按钮并尝试使用 isselected 方法获取布尔值时,我总是得到错误值。
为什么会这样。请帮助我。
【问题讨论】:
标签:
android
radio-button
android-radiogroup
【解决方案1】:
我也遇到了同样的问题,isSelected 方法不起作用。
isChecked 方法对我有用:
if( myRadioButton.isChecked() ){
// do stuff
} else {
// do other stuff
}
【解决方案2】:
使用这个:
int selectedRbBtn = radiogroup.getCheckedRadioButtonId();
如果它返回 -1 则没有选择任何内容...
【解决方案3】:
也许这会有所帮助
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
然后使用单选按钮执行您想要的任何任务。
来源:Link
【解决方案4】:
使用这个:
radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton btn, boolean isCheck) {
//handle the boolean flag here.
if(isCheck==true)
//Do something
else
//do something else
}
});
来源:link