我正在研究安卓。在我的页面中,我有单选按钮,我需要获取选定的单选按钮值。我的问题是我只能在 onCheckedChanged 方法中获取选定的单选按钮值。但是,当我尝试在 onCheckedChanged 方法之外打印值时,它不会打印。我怎样才能获得该方法之外的价值?这个你能帮我吗。我也需要外面的价值。
My code:
radiogroup = (RadioGroup) new RadioGroup(Page.this);
layout.addView(radiogroup);
for (int p = 0; p < arr.length; p++) {
RadioButton newRadioButton = new RadioButton(Page.this);
newRadioButton.setText(stringList6.get(p));
newRadioButton.setTextColor(Color.parseColor("#000000"));
radbtn = Integer.parseInt(stringList5.get(p));
newRadioButton.setId(radbtn);
radiogroup.addView(newRadioButton);
}
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup radioGroup,
int checkedId) {
for (int h = 0; h < radioGroup.getChildCount(); h++) {
RadioButton btn = (RadioButton) radioGroup.getChildAt(h);
if (btn.getId() == checkedId) {
selectedop1 = btn.getId();
System.out.println("selected radio button inside"+ selectedop1);
return;
}
}
}
});
System.out.println("selected radio button outside"+ selectedop1);