动态添加单选按钮组

发布于 2024-08-17 08:26:37 字数 207 浏览 5 评论 0原文

我正在尝试在 Flex 中进行测验,并从 xml 文件加载数据。对于每个问题,我想创建一个单选按钮组,以便我可以将单选按钮与其关联。我怎样才能用动作脚本来实现这一点?我可以看到 addChild 方法适用于 DisplayObjects,并且我认为 radiobuttongroup 不是一个方法,因为我收到错误。如何在 Flex 应用程序中使用 ActionScript 动态添加单选按钮组?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

注定孤独终老 2024-08-24 08:26:37

如果将单选按钮添加到 FormItem,它们会自动分组在一起。因此,假设您的测验使用 Flex Form 进行布局,您只需为每个问题生成一个 FormItem,为每个选项添加一个按钮到 FormItem,然后将该 FormItem 添加到您的主 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);
        }
}

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);
        }
}
云柯 2024-08-24 08:26:37

您创建单选按钮,将它们添加到显示中,为它们创建一个组并声明单选按钮属于同一组 (RadioButton.group = group1)。 RadioButtonGroup 确实不是一个显示对象,而只是告诉属于该组的单选按钮它们应该充当一个元素。

http://livedocs.adobe.com/flash/9.0/ ActionScriptLangRefV3/fl/controls/RadioButtonGroup.html

http ://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/RadioButton.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文