flex 事件机制实解
主页面:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();" layout="absolute">
<mx:Script>
<![CDATA[
import myComponents.CustomEvent;
]]>
</mx:Script>
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
import myComponents.titleWin;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
private function init():void{
addEventListener("test",handlerTEST,true);
var dataProvider:ArrayCollection = new ArrayCollection([
{name:"lixx1",task:"lixx1"},{name:"lixx2",task:"lixx2"},{name:"lixx3",task:"lixx3"}]);
this.myDatagrid.dataProvider = dataProvider;
}
private function handlerTEST(event:CustomEvent):void{
//Alert.show(""+(event as CustomEvent).getOBJ.name);
var win:titleWin = titleWin(PopUpManager.createPopUp(this,titleWin,true)) ;
win.userName.text = "lixx";
win.password.text = "lixx";
PopUpManager.centerPopUp(win);
}
]]>
</mx:Script>
<mx:DataGrid id="myDatagrid">
<mx:columns>
<mx:DataGridColumn headerText="姓名" dataField="name">
</mx:DataGridColumn>
<mx:DataGridColumn headerText="任务" dataField="task">
</mx:DataGridColumn>
<mx:DataGridColumn headerText="操作">
<mx:itemRenderer>
<mx:Component>
<mx:Button label="TEST" click="cli();">
<mx:Script>
<![CDATA[
import myComponents.CustomEvent;
private function cli():void{
dispatchEvent(new CustomEvent("test",data));
}
]]>
</mx:Script>
</mx:Button>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Application>
窗体页面:
<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal"
creationComplete="init();" width="400" height="300" showCloseButton="true"
close="remove(event);">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.CloseEvent;
private function init():void{
this.submit.addEventListener(MouseEvent.CLICK,submit1);
this.cancel.addEventListener(MouseEvent.CLICK,cancel1);
}
private function remove(event:CloseEvent):void{
}
private function submit1(event:MouseEvent):void{
Alert.show("success login");
}
private function cancel1(event:MouseEvent):void{
Alert.show("success logout");
}
]]>
</mx:Script>
<mx:Form id="myForm">
<mx:FormHeading label="用户登录">
</mx:FormHeading>
<mx:FormItem label="用户名: ">
<mx:TextInput id="userName" text="" />
</mx:FormItem>
<mx:FormItem label="密码: ">
<mx:TextInput id="password" text="" />
</mx:FormItem>
<mx:FormItem direction="horizontal">
<mx:Button label="submit" id="submit"/>
<mx:Button label="cancel" id="cancel"/>
</mx:FormItem>
</mx:Form>
</mx:TitleWindow>