2015-11-17 43 views
0

我在测验应用程序中正确使用几个RadioButton组件和一个RadioButtonGroup(当它们通过DataGridColumn下的ItemRenderer呈现时)有一些问题。在DataGridColumn中使用RadioButton和RadioButtonGroup(在测验应用程序中)

测验页面显示一个问题以及由RadioButton组件表示的4个可能答案(与唯一的RadioButtonGroup绑定)。测验通常包含许多选择题。

一切正常,直到我导航到下一个问题,并返回到上一个问题;那么我可以在单选按钮中看到我以前的答案选择,但是当我更改我的答案并选择另一个单选按钮时,它会保留我的旧选择并添加新选项,这是完全不合理的(因为RadioButtonGroup应该只强制选择一个选项) 。

这里是我定义的DataGridColumn:

<mx:DataGrid y="141" height="373" dataProvider="{currentQuestion.answers}" id="answersGrid" visible="false" styleName="NoStyleDataGrid" useRollOver="false" headerHeight="0" width="381" x="461" variableRowHeight="true">   
      <mx:columns>         
      <mx:DataGridColumn width="20" dataField="key"> 
       <mx:itemRenderer> 
         <mx:Component> 
         <mx:Canvas width="100%" height="100%" verticalScrollPolicy="off" horizontalScrollPolicy="off" top="0"> 
          <mx:Script> 
           <![CDATA[ 
            import mx.core.Application; 
           ]]> 
          </mx:Script> 

          <mx:RadioButton id="rb" value="{data.number}" selected="{outerDocument.isSelected(data.number,rb)}" group="{outerDocument.getGroup()}" click="outerDocument.setAnswerState(event,data.number)" width="100%" height="30" x="12" y="0" />         

         </mx:Canvas>          
         </mx:Component> 
       </mx:itemRenderer> 
      </mx:DataGridColumn> 

我要确保每个问题都有自己独特的组使用此代码包含单选按钮组件(如答案):

 public function getGroup():RadioButtonGroup{     
      if(radioGroupMap[currentQuestion.key]!=null){          
       return radioGroupMap[currentQuestion.key] as RadioButtonGroup;          
      }        
      radioGroupMap[currentQuestion.key] = new RadioButtonGroup(); 
      return radioGroupMap[currentQuestion.key] as RadioButtonGroup; 
     } 

这代码工作正常,只要我不去下一个问题,并返回到前一个问题。

感谢有关此问题的任何帮助。

谢谢!

+0

如果你能证明问题的屏幕快照或添加样品运行的应用程序,这将是清楚明白您的问题。 – gbdcool

+0

我的第一个问题是,为什么你用渲染器和一个DataGrid,而不是普通的集装箱,radiobuttongroups :)而第二个问题 - 当你说“我浏览了下一个问题,回到前面的问题:”你的意思是滚动数据网格?因为如果是这样的话,这可能是因为itemrenderers始终被重用 - 当您滚动并且itemrenderer离开屏幕时,它不会被删除,而会重新用于新项目。所以,当你上下滚动时,可能会发生以下问题。 – Philarmon

+0

也许我应该使用简单的容器,我最初使用datagrid,因为我显示的答案数量未知,我需要每个答案都包含答案号/字母,内容和错误/正确的指示。这似乎是数据网格的经典使用。我可以使用其他容器,但由于答案的数量在不同问题之间变化,所以我将不得不动态构建UI。 – Uri

回答

0

做一些研究后,将显示一个包含与数据网格列的RadioButtonGroup沿单选按钮,项目渲染器是不是一个很好的设计选择。由于性能方面的原因,显然会创建比实际需要更多的项目渲染器,这使得UI以不可预知的方式发挥作用。

我使用了VBOX,并使用ActionScript动态创建了RadioButton和RadioButtonGroup。下面是一些代码示例:

  function populateAnswers(){ 
       var rbg:RadioButtonGroup = new RadioButtonGroup(); 
       for each (var ans:Answer in currentQuestion.answers){ 
        var row:HBox = new HBox(); 
        var rb:RadioButton = new RadioButton(); 
        rb.group = rbg; 
        rb.id = ""+ans.number; 
        var num:int = ans.number; 
         rb.addEventListener(MouseEvent.CLICK,setAnswerState); 
       rb.selected = isSelected(ans.number);      
         row.addChild(rb);      

        var answerBoby:Text = new Text(); 
        answerBoby.enabled = false; 
        answerBoby.styleName = "readOnlyTA"; 
        answerBoby.selectable = false; 
        answerBoby.htmlText = ans.body; 
        row.addChild(answerBoby); 
        quizAnswersPanel.addChild(row);      

       } 
相关问题