Flex组件组件添加自定义事件
2013-01-08 13:45
288 查看
自定义组件如下:com.cp2
<?xml version="1.0" encoding="utf-8"?> <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="254" height="218"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Metadata> /* 在这里定义事件 */ [Event(name="shareData",type="flash.events.TextEvent")] [Event(name="clickMe",type="flash.events.Event")] </fx:Metadata> <fx:Script> <![CDATA[ protected function button1_clickHandler(event:MouseEvent):void { var textEvent:TextEvent=new TextEvent("shareData"); textEvent.text="hello world.";//传递简单的参数 dispatchEvent(textEvent); } protected function button2_clickHandler(event:MouseEvent):void { dispatchEvent(new TextEvent("clickMe")); //不传递参数 } protected function button3_clickHandler(event:MouseEvent):void { this.owner.dispatchEvent(new Event("clickMeToo")); } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Button x="40" y="10" label="单击我(传递参数)" width="190" height="45" click="button1_clickHandler(event)"/> <s:Button x="39" y="74" label="单击我" width="190" height="37" click="button2_clickHandler(event)"/> <s:Button x="39" y="139" label="按钮" width="190" height="35" click="button3_clickHandler(event)"/> </s:Group>主文件
<?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:com="com.*" creationComplete="application1_creationCompleteHandler(event)"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; protected function application1_creationCompleteHandler(event:FlexEvent):void { //this.addEventListener("clickMeToo",handler1);//因为在cp1里,发送事件的是cp1.owner.因此,此处添加监听无效,应该是直接、第一个拥有着 bc.addEventListener("clickMeToo",handler1); cpID.addEventListener("shareData",handler); } private function handler(event:TextEvent):void{ Alert.show("增加监听的方式 ,并传递参数。"+event.text); } private function handler1(event:Event):void{ Alert.show("增加监听的方式 "); } protected function xxx_shareDataHandler(event:TextEvent):void { Alert.show(event.text); } //在组件中添加事件监听 protected function cpID_clickMeHandler(event:Event):void { Alert.show("事件传递成功"); } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:BorderContainer id="bc" x="141" y="38" width="299" height="303" borderColor="#A10F0F" dropShadowVisible="true" chromeColor="#B19191" contentBackgroundColor="#FFFFFF" borderWeight="4" rollOverColor="#164FA4"> <com:cp2 x="22" y="31" id="cpID" clickMe="cpID_clickMeHandler(event)" > </com:cp2> </s:BorderContainer> </s:Application>另外可自定义事件,传递复杂参数
相关文章推荐
- 在Flex中给HSlider组件添加滑块个数
- 如何给flex 的组件添加阴影
- 通过ACTIONSCRIPT给FLEX中的组件添加特效.
- FLEX中给组件添加自定义事件
- 别指望在NativeWindow里添加Flex组件
- flex datagrid组件中添加别的组件
- Flex之旅:第二部分:容器与布局(2)---动态添加组件(TabNavigator)
- 将flex组件添加到papervision3d
- flex 组件添加鼠标手型
- flex自定义组件如何在设计时显示预期结果/已添加控件
- flex chart 上添加其他组件的方法。
- 为Flex自定义组件添加属性和事件
- Flex 4 NativeWindow 中添加Flex组件(问题很多,尚不完善)
- Flex中添加大量组件时内存占用问题
- 向FLEX容器组件中添加sprite
- Flex自定义事件和组件的使用方法
- Flex中添加大量组件时内存占用问题
- FLEX中给组件添加自定义事件
- 动态添加组件到flex的可折叠容器中
- flex 任意组件添加鼠标手型