gpt4 book ai didi

android - 如何检查在android中选中了哪个单选按钮?

转载 作者:行者123 更新时间:2023-11-29 20:19:14 26 4
gpt4 key购买 nike

大家好,我有 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;


}

但它不起作用,我正在将类型分配给方法之外的另一个字符串

最佳答案

尝试实现以下代码

 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);
}
});

关于android - 如何检查在android中选中了哪个单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33464995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com