[英]how to check which radio button is checked in android?
Hi guys i have a 4 values ie 4 radio buttons which i want to check if they are checked or not how will i do that ?嗨,伙计们,我有 4 个值,即 4 个单选按钮,我想检查它们是否被选中,我该怎么做? i have tried this
我试过这个
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;
}
But its not working and i am assigning the type to another string outside the method但它不起作用,我将类型分配给方法之外的另一个字符串
try implementing the following code尝试实现以下代码
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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.