禁用radiobuttongroup

时间:2010-01-13 20:49:55

标签: flex flex3

由于Repeater组件不会在mxml中生成radiobuttongroup,并且因为我无法通过ActionScript执行相同操作,因为当我尝试以这种方式创建时,radiobuttongroup没有id属性,是否有办法禁用radiobuttons什么?据我所知,我唯一可以设置和访问的是radiobuttons的groupName属性。

对于Repeater组件,我尝试直接使用xml

<mx:XML id="xquiz" source="quiz.xml" />

使用此代码:

<mx:Repeater dataProvider="{xquiz.question}" id="rep">
        <mx:Label text="{rep.currentItem.content}" />
        <mx:Repeater dataProvider="{rep.currentItem.answer}" id="rep2">
            <mx:RadioButton label="{rep2.currentItem}" groupName="{'rbg'+rep.currentIndex}" click="checkAnswers(event)" value="{rep2.currentItem.@correct}" />
        </mx:Repeater>
    </mx:Repeater>

并且不能使用中继器来生成radiobuttongroup,因为它不是可见组件。

我也尝试过ActionScript方法,但使用HTTPService mxml组件来获取xml文件。

<mx:HTTPService id="srv" url="quiz.xml" resultFormat="e4x" result="handleResult(event);" fault="handleFault(event);"/>

和这里的actionscript片段:

private var xQuizData:XML

private function handleResult(event:ResultEvent):void {
                xQuizData = event.result as XML;
                initApp();
            }

private function initApp():void {
                var cnt:Number = 0;
                for each (var question:XML in xQuizData.*) {
                    var lbl:Label = new Label();
                    lbl.text = question.content;
                    panel.addChild(lbl);
                    lbl.visible = true;
                    var cnt2:Number = 0;
                    for each (var answer:XML in question.answer) {
                        var rb:RadioButton = new RadioButton();
                        rb.id=String(cnt);
                        rb.label=answer;
                        rb.groupName="rbg"+String(cnt);
                        if (answer.hasOwnProperty("correct")) {
                            rb.value=true;
                        }
                        panel.addChild(rb);
                        rb.visible = true;
                        cnt2++;
                    }
                    cnt++;
                }
            }

我希望能够捕获来自radiobuttongroup控件的点击,但是如果使用转发器则无法生成它们,或者如果使用actionscript则无法为它们分配id。

XML内容看起来像这样:

<quiz>
<question>
<content>Some question?</content>
<answer>Answer one</answer>
<answer correct="true">Answer two</answer>
<answer>Answer three</answer>
<answer>Answer four</answer>
</question>
</quiz>

2 个答案:

答案 0 :(得分:0)

我很难从你的片段中跟踪你想要做的事情,所以这里有一个我应该完全符合你想要的片段。希望您可以查看它并根据您的问题进行调整。

public function buildVBox():void{
    tempVBox.removeAllChildren();
    var iterator:Number = 0;

    for each (var i:XML in myXML.children()){

        var tempCanv:Canvas = new Canvas();
        tempCanv.id = iterator.toString();
        var tempRadYes:RadioButton = new RadioButton;
        tempRadYes.x = 30; 
        tempRadYes.y = 20;
        tempRadYes.origY = 20;
        tempRadYes.groupName = "rbg" + iterator.toString();
        tempRadYes.value = 1; 
        tempRadYes.label = "Yes"
        tempCanv.addChild(tempRadYes);
        var tempRadNo:extRadioButton = new extRadioButton;
        tempRadNo.x = 80; 
        tempRadNo.y = 20;
        tempRadNo.origY = 20;
        tempRadNo.groupName = "rbg" + iterator.toString();
        tempRadNo.value = 2; 
        tempRadNo.label = "No"
        tempCanv.addChild(tempRadNo);
        var tempRadNA:extRadioButton = new extRadioButton;
        tempRadNA.x = 120; 
        tempRadNA.y = 20;
        tempRadNA.origY = 20;
        tempRadNA.groupName = "rbg" + iterator.toString();
        tempRadNA.value = 0; tempRadNA.label = "N/A"
        tempCanv.addChild(tempRadNA);
        tempVBox.addChild(tempCanv);
        iterator++;
    }
}

答案 1 :(得分:0)