大家好我有4个值,即4个单选按钮,我想检查它们是否被检查,我将如何做?我试过这个
public void onRadioButtonClicked(View v){
// Your code on click
radioTypeGroup = (RadioGroup) findViewById(R.id.radioType);
radioTypeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int selectedId = radioTypeGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
RadioButton r1 = (RadioButton) findViewById(R.id.radio_food);
RadioButton r2 = (RadioButton) findViewById(R.id.radio_fitness);
RadioButton r3 = (RadioButton) findViewById(R.id.radio_entertainment);
RadioButton r4 = (RadioButton) findViewById(R.id.radio_events);
if (r1.isChecked()) {
String food = "food";
type = food;
}
if (r2.isChecked()) {
String fitness = "fitness";
type = fitness;
}
但它不起作用,我将类型分配给方法
之外的另一个字符串答案 0 :(得分:2)
尝试实现以下代码
RadioGroup group= (RadioGroup) getView().findViewById(R.id.radioGroup);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
View radioButton = radioGroup.findViewById(i);
int index = radioGroup.indexOfChild(radioButton);
}
});