I'm trying to make a quiz in flex and am loading data from an xml file. For each question I want to create a radiobuttongroup so I can associate radio buttons to it. How can I accomplish that with actionscript? I can see that addChild method works for DisplayObjects and I presume that radiobuttongroup is not one because I'm receiving errors. How can I dynamically add radiobuttongroup with actionscript in flex application? Thanks.
You create the radio buttons, add them to the display, create a group for them and declare the radio buttons to belong to the same group (RadioButton.group = group1
). The RadioButtonGroup is indeed not a display object but just tells the radio buttons belonging to that group that they should acts as one element.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/RadioButtonGroup.html
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/RadioButton.html
If you add radio buttons to a FormItem, they are automatically grouped together. So, assuming your quiz uses a Flex Form for layout, you simply generate a FormItem for each question, add a button for each option to the FormItem, then add that FormItem to your main Form.
private function generateQuestions(questions:XML):void
{
var form:Form = new Form();
this.addChild(form);
for each (var question:XML in questions.question)
{
var questionItem:FormItem = new FormItem();
form.addChild(questionItem);
questionItem.label = question.@text;
for each (var option:XML in question.option)
{
var optionButton:RadioButton = new RadioButton();
optionButton.label = option.@text;
questionItem.addChild(optionButton);
}
}