2012-12-12 24 views
1

我試圖做很多方法,但對於我的生活無法想象如何。基本上我有一個列表。選擇列表中的項目時,單選按鈕標籤會發生變化。但是,我希望一個標籤和文本區域出現,一旦用戶點擊了單選按鈕(如果它是正確的)。 到目前爲止的代碼: - 從列表組件到單選按鈕的Adobe flex

<s:VGroup x="103" y="130" width="123" height="125"> 
    <s:RadioButton id="RadioButton1" label="{data.QuestionsRadioButton1}" groupName="QuestionsTestRadioButtons" click="RadioButton1_clickHandler(event)"/> 
    <s:RadioButton label="{data.QuestionsRadioButton2}" groupName="QuestionsTestRadioButtons" click="radiobutton1_clickHandler(event)" /> 
    <s:RadioButton id="RadioButton3" label="{data.QuestionsRadioButton3}" groupName="QuestionsTestRadioButtons" click="radiobutton2_clickHandler(event)"/> 
</s:VGroup> 

我不會發布的所有代碼,我們將永遠在這裏。然而,有沒有辦法可能是一個if函數?說出單擊的單選按鈕實際上是否是正確的答案?

任何建議將是有益的 謝謝

+0

不知道你的數據結構;或者如何顯示單選按鈕我們怎麼樣 - 當堆棧溢出問題的答案 - 應該知道是否點擊了正確的項目?然後將其擴展到Flex;框架應該如何知道?除非你告訴它,否則它不會。如果你有動態的問題/答案;您需要以某種方式加載正確的答案,然後他們與選定的答案進行比較。想必你會在單選按鈕上設置一個「值」,你可以用它來比較「正確」的答案。 – JeffryHouser

回答

0

我試圖起草一個完整的例子:

Questionaire.mxml

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:local="*"> 
    <fx:Declarations> 
     <s:ArrayCollection id="questions"> 
      <local:Question text="It is raining …"> 
       <s:ArrayCollection> 
        <local:Answer text="Men"/> 
        <local:Answer text="Cats and dogs" correct="true"/> 
        <local:Answer text="Candy"/> 
       </s:ArrayCollection> 
      </local:Question> 
      <local:Question text="The sky is …"> 
       <s:ArrayCollection> 
        <local:Answer text="Blue" correct="true"/> 
        <local:Answer text="Orange" correct="true"/> 
        <local:Answer text="Grey" correct="true"/> 
        <local:Answer text="Green"/> 
       </s:ArrayCollection> 
      </local:Question> 
     </s:ArrayCollection> 
    </fx:Declarations> 

    <s:DataGroup dataProvider="{questions}" itemRenderer="QuestionRenderer"> 
     <s:layout> 
      <s:VerticalLayout gap="24"/> 
     </s:layout> 
    </s:DataGroup> 
</s:Application> 

QuestionRenderer.mxml

<?xml version="1.0" encoding="utf-8"?> 
<s:DataRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark"> 
    <fx:Script> 
     <![CDATA[ 
      [Bindable("dataChange")] 
      public function get question():Question { 
       return data as Question; 
      } 
     ]]> 
    </fx:Script> 

    <fx:Declarations> 
     <s:RadioButtonGroup id="answerGroup"/> 
    </fx:Declarations> 

    <s:layout> 
     <s:VerticalLayout/> 
    </s:layout> 

    <s:Label fontWeight="bold" text="{question.text}"/> 

    <s:DataGroup dataProvider="{question.answers}"> 
     <s:layout> 
      <s:HorizontalLayout/> 
     </s:layout> 
     <s:itemRenderer> 
      <fx:Component> 
       <s:DataRenderer> 
        <fx:Script> 
         <![CDATA[ 
          import spark.components.RadioButtonGroup; 

          public function get answerGroup():RadioButtonGroup { 
           return outerDocument.answerGroup; 
          } 
         ]]> 
        </fx:Script> 
        <s:RadioButton groupName="answerGroup" label="{data.text}" value="{data}"/> 
       </s:DataRenderer> 
      </fx:Component> 
     </s:itemRenderer> 
    </s:DataGroup> 

    <s:Label visible="{answerGroup.selectedValue}" text="This is {answerGroup.selectedValue.correct ? 'correct' : 'incorrect'}."/> 
</s:DataRenderer> 

Question.as

package { 
    import mx.collections.ArrayCollection; 

    [Bindable] 
    [DefaultProperty("answers")] 
    public class Question { 
     public var text:String; 
     public var answers:ArrayCollection; 
    } 
} 

Answer.as

package { 
    [Bindable] 
    public class Answer { 
     public var text:String; 
     public var correct:Boolean = false; 
    } 
}