问题
Hi i created dynamic radiogroup with radiobutton.Now i want to get values from dynamic radiogroup. Below is my code
final RadioGroup rg = new RadioGroup(this);
rg.setId(questionNo);
RadioButton[] rb = new RadioButton[answers.length()];
for (int i = 0; i < answers.length(); i++) {
JSONObject answerObject = answers.getJSONObject(i);
rb[i] = new RadioButton(this);
rb[i].setText(answerObject.getString("AnswerValue"));
rb[i].setId(answerObject.getInt("AnswerId"));
rg.addView(rb[i]);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedId = rg.getCheckedRadioButtonId();
Log.i("ID", String.valueOf(selectedId));
}
});
Button click event
submit_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rg.getCheckedRadioButtonId()!=-1){
int id= rg.getCheckedRadioButtonId();
View radioButton = rg.findViewById(id);
int radioId = rg.indexOfChild(radioButton);
RadioButton btn = (RadioButton) rg.getChildAt(radioId);
String selection = (String) btn.getText();
Log.i("selection", selection);
}
}
});
I am getting only last index of the radio group.
回答1:
You Can Achive This Soln By This Way
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
void onCheckedChanged(RadioGroup rg, int checkedId) {
for(int i=0; i<rg.getChildCount(); i++) {
RadioButton btn = (RadioButton) rg.getChildAt(i);
if(btn.getId() == checkedId) {
String text = btn.getText();
// do something with text
return;
}
}
}
});
回答2:
I know it's too late, But this may help someone else
While creating your Dynamic Radiobutton Group, Create a parent layout(Linear layout or Relative Layout)
All radio groups will be the child of that Layout and all radio button will be a child of the specific radio group.
Creating No of Dynamic Radio Group
- Create all Dynamic radio group in a parent layout
- Add all radio button to a group
Add all radio group to the parent layout
public RadioButton[] rb; public RadioGroup grp; public LinearLayout layoutmain; //Parent layout for(int i=0;i<qus.size();i++) //No of Questions { rb = new RadioButton[size]; grp = new RadioGroup(this); grp.setId(a+qus.get(i).qusetionId); for(int j=0;j<qus.get(i).choices.size();j++) //No of Radio button in radio group { rb[j] = new RadioButton(this); rb[j].setText(qus.get(i).choices.get(j).getChoiceText()); grp.addView(rb[j]); // adding button to group } layoutmain.addView(grp); // adding group to layout }
Retrieve All selected button task
- From parent, layout get all child
- Find all radio group using the child Instance
- Create an object for that radio group
using that object get the selected radio button
Button bt = (Button) findViewById(R.id.button); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { for(int i=0;i<layoutmain.getChildCount();i++) //From the parent layout (Linear Layout) get the child { View child = layoutmain.getChildAt(i); if(child instanceof RadioGroup) //Check weather its RadioGroup using its INSTANCE { RadioGroup rg = (RadioGroup) child; //create a RadioButton for each group int selectedId = rg.getCheckedRadioButtonId(); // get the selected button RadioButton radiochecked = (RadioButton) findViewById(selectedId); Log.e("Radio",radiochecked.getText().toString()); //from the button get the selected text } } } });
来源:https://stackoverflow.com/questions/30627623/how-to-get-values-from-dynamic-radiogroup-android