【问题标题】:Using RadioButton with RadioButtonGroup in a DataGridColumn (in a quiz application)在 DataGrid 列中使用单选按钮和单选按钮组(在测验应用程序中)
【发布时间】:2023-09-19 17:00:01
【问题描述】:

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

测验页面显示一个问题以及 4 个可能的答案,这些答案由 RadioButton 组件表示(与唯一的 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>

我使用以下代码确保每个问题都有自己独特的组来包含 RadioButton 组件(作为答案):

        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;
        }

只要我不转到下一个问题并返回上一个问题,此代码就可以正常工作。

感谢您对此问题的任何帮助。

谢谢!

【问题讨论】:

  • 如果您可以显示问题的屏幕截图或添加示例正在运行的应用程序,那么您的问题就很清楚了。
  • 我的第一个问题是为什么你使用 itemrenderers 和数据网格而不是带有单选按钮组的普通容器:) 第二个问题 - 当你说“我导航到下一个问题并返回上一个问题”时你的意思是滚动数据网格?因为如果是这样,这可能是因为 itemrenderer 一直被重用 - 当您滚动并且 itemrenderer 离开屏幕时,它不会被删除,而是重用于新项目。因此,当您向上和向下滚动时,可能会出现以前问题中的一些内容。
  • 也许我应该使用普通容器,我最初使用数据网格,因为我显示未知数量的答案,我需要每个答案包含答案编号/字母、内容和错误/正确指示。这似乎是数据网格的经典用法。我可以使用其他容器,尽管我必须动态构建 UI,因为答案的数量在问题之间会发生变化。

标签: actionscript-3 apache-flex flex3 mxml


【解决方案1】:

在做了一些研究之后,似乎在数据网格列上包含 RadioButtons 和 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);                     

                }

【讨论】: