Flex控件之repeater和radioButton应用
代码:http://www.cnblogs.com/modou/articles/2108346.html
<?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:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; protected function button1_clickHandler(event:MouseEvent):void { Alert.show(radiogroup1.selectedValue.toString()); } protected function radiobutton1_creationCompleteHandler(event:FlexEvent):void { var rb:RadioButton =event.currentTarget as RadioButton; if(rb==radiogroup1.getRadioButtonAt(2)) { rb.selected = true; } } ]]> </fx:Script> <fx:Declarations> <fx:Array id="arr"> <fx:Object label="北京" data="010" /> <fx:Object label="上海" data="021" /> <fx:Object label="天津" data="022" /> <fx:Object label="重庆" data="023" /> <fx:Object label="广州" data="020" /> </fx:Array> <s:RadioButtonGroup id="radiogroup1" /> </fx:Declarations> <s:Button x="336" y="316" label="按钮" click="button1_clickHandler(event)"/> <mx:VBox x="100" y="200" height="200" width="200" borderVisible="true" borderStyle="outset"> <mx:Repeater dataProvider="{arr}" id="radioRepeater" x="52" y="38"> <s:RadioButton label="{radioRepeater.currentItem.label}" value="{radioRepeater.currentItem.data}" groupName="radiogroup1"
creationComplete="radiobutton1_creationCompleteHandler(event)" /> </mx:Repeater> </mx:VBox> </s:Application>
1.首先先用fx:Array生成数据对象,fx:Array需要放在fx:Declarations声明部分
2.添加一个s:RadioButtonGroup,也需要放在fx:Declarations声明部分
3.添加一个mx:Repeater组件,并在mx:Repeater组件内添加一个s:RadioButton组件给mx:Repeater组件设置数据源,那么数据源有几条数据,就会生成几个RadioButton
由于mx:Repeater组件必须包在基于Halo的容器中,所以还需要在mx:Repeater组件外包一个mx:VBox组件
4.通过radiogroup1.selectedValue.toString(),可以获取当前选择项的value值
5.如果需要默认绑定第一个RadioButton,可以在RadioButton组件添加creationComplete事件,监测如果是第一项,就选中