flex4.6事件分派+组件+参数传递
<?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" xmlns:hz="com.hz.*" creationComplete="initHandler(event)"> <fx:Script> <![CDATA[ import Event.AddStuEvent; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.events.FlexEvent; protected function initHandler(event:FlexEvent):void { stuInfo.stuColl=new ArrayCollection(); addEventListener(AddStuEvent.ADD_USER,addBtn); } protected function addBtn(event:AddStuEvent):void { if(stuMana.stuName.text!=""&&stuMana.stuAge.text!="") { stuInfo.stuColl.addItem(event.object); } else { Alert.show("两项都要填三!","温馨提示"); } } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:layout> <s:HorizontalLayout gap="30" paddingBottom="30" paddingLeft="30" paddingRight="30" paddingTop="30"/> </s:layout> <hz:StuInfo id="stuInfo" width="50%" height="100%"/> <hz:StuManager id="stuMana" width="50%" height="100%"/> </s:Application>
上面是主application文件: <?xml version="1.0" encoding="utf-8"?> <s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" title="添加学生" creationComplete="initHandler(event)"> <fx:Script> <![CDATA[ import Event.AddStuEvent; import mx.events.FlexEvent; public function initHandler(event:FlexEvent):void { addBtn.addEventListener(MouseEvent.CLICK,addBtnHandler); } protected function addBtnHandler(event:MouseEvent):void { var object:Object=new Object(); object={sName:stuName.text,sAge:stuAge.text}; dispatchEvent(new AddStuEvent(AddStuEvent.ADD_USER,object)) } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Form width="100%" height="100%"> <s:FormItem label="学生姓名:" required="true" width="100%"> <s:TextInput id="stuName" width="100%"/> </s:FormItem> <s:FormItem label="学生年龄" required="true" width="100%"> <s:TextInput id="stuAge" width="100%"/> </s:FormItem> </s:Form> <s:controlBarContent> <s:Button label="添加学生:" id="addBtn"/> <s:Button label="修改信息" id="updateBtn"/> </s:controlBarContent> <s:controlBarLayout> <s:HorizontalLayout horizontalAlign="center" gap="50" paddingTop="9" paddingBottom="9"/> </s:controlBarLayout> </s:Panel>
下面是自定义事件:定义了一个常量和要传递的参数,event属性只设置了string,bubble默认为false。
package Event { import flash.events.Event; public class AddStuEvent extends Event { public static const ADD_USER:String="addUser"; private var _object:Object=null; public function AddStuEvent(type:String, object:Object) { super(type,true); _object=object; } public function get object():Object { return _object; } public function set object(value:Object):void { _object = value; } override public function clone():Event { return new AddStuEvent(type,object); } } }
<?xml version="1.0" encoding="utf-8"?> <s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" title="学生列表" creationComplete="initHandler(event)"> <fx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.events.FlexEvent; [Bindable] public var stuColl:ArrayCollection=null; protected function initHandler(event:FlexEvent):void { // TODO Auto-generated method stub } private function stuArr(item:Object):String { var str:String=item.sName+"哈哈"+item.sAge; return str; } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:List id="stuList" width="100%" height="100%" dataProvider="{stuColl}" labelFunction="stuArr"> </s:List> <s:controlBarContent> <s:Button label="删除学生" id="deleteBtn"/> </s:controlBarContent> </s:Panel>