flex 动态创建复选框

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" x="46" y="42" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import mx.controls.CheckBox;

private function initApp():void
{
var items:Array = ["apple","boy","cat","dog","eye","fox"];

for(var i:int=0;i<items.length;i++)
{
var checkBox:CheckBox = new CheckBox();
checkBox.id = "checkBox" + i.toString();
checkBox.label = items[i].toString();
checkBox.setStyle("fontSize",14);
checkBox.x = 100;
checkBox.y = (i + 1) * 25;
checkBox.addEventListener(Event.CHANGE,checkBox_change);
this.addChild(checkBox);//也可是this.gridItem.addChild(checkBox)
}   
}

private function checkBox_change(e:Event):void
{
if(e.target.selected)
{
lbItems.text = lbItems.text+e.target.label+" ";
}
else
{
lbItems.text = lbItems.text.replace(e.target.label,"");
}
}
]]>
</mx:Script>
<mx:Label id="lbItems" x="56" y="245" text="Your choice is: " width="345" fontSize="14"/>
</mx:WindowedApplication>

posted @ 2012-08-27 22:48  IT忍者  阅读(711)  评论(0编辑  收藏  举报