zoukankan      html  css  js  c++  java
  • flex中Event类的使用

    当您创建自己的自定义 Event 类时,必须覆盖继承的 Event.clone() 方法,以复制自定义类的属性。如果您未设置在事件子类中添加的所有属性,则当侦听器处理重新分派的事件时,这些属性将不会有正确的值。

    自定义事件类 继承flash.events.Event类 下面看代码 其中 message 是自定义的属性, 下面要使用这个属性 来传递参数

    [java]      view plain    copy   
     
    1. package com.demo.event  
    2. {  
    3.     import flash.events.Event;  
    4.       
    5.     public class TestEvent extends Event  
    6.     {  
    7.         public static const EVENT_CLICK:String = "copy_text";  
    8.           
    9.         public var message:String;  
    10.           
    11.         public function TestEvent(type:String, message:String)  
    12.         {  
    13.             super(type);  
    14.             this.message = message;  
    15.         }  
    16.           
    17.         override public function clone():Event{    
    18.               
    19.             return new TestEvent(type,message);    
    20.               
    21.         }    
    22.     }  
    23. }  

    接下来建立一个控件 来指派这个事件
    注册事件 CopyText

    <fx:Metadata>   [Event(name="CopyText",type="com.demo.event.TestEvent")]  </fx:Metadata> 指派事件

    protected function button1_clickHandler(event:MouseEvent):void    {     dispatchEvent(new TestEvent("CopyText",tempText.text));    }

    [c-sharp]      view plain    copy   
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"   
    3.          xmlns:s="library://ns.adobe.com/flex/spark"   
    4.          xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">  
    5.     <s:layout>  
    6.         <s:BasicLayout/>  
    7.     </s:layout>  
    8.     <fx:Metadata>  
    9.         [Event(name="CopyText",type="com.demo.event.TestEvent")]  
    10.     </fx:Metadata>  
    11.   
    12.     <fx:Script>  
    13.         <!--[CDATA[  
    14.             import com.demo.event.TestEvent;  
    15.             protected function button1_clickHandler(event:MouseEvent):void  
    16.             {  
    17.                 dispatchEvent(new TestEvent("CopyText",tempText.text));  
    18.             }  
    19.         ]]-->  
    20.     </fx:Script>  
    21.   
    22.     <fx:Declarations>  
    23.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->  
    24.     </fx:Declarations>  
    25.     <s:TextInput x="10" y="10" height="107" width="260" id="tempText"/>  
    26.     <s:Button x="14" y="124" label="Copy" click="button1_clickHandler(event)"/>  
    27. </s:Group>  

     

    最后将这个控件放到主程序中, 并使用了这个自定义事件

    [c-sharp]      view plain    copy   
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
    3.                xmlns:s="library://ns.adobe.com/flex/spark"   
    4.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:components="com.demo.view.components.*">  
    5.     <s:layout>  
    6.         <s:BasicLayout/>  
    7.     </s:layout>  
    8.     <fx:Script>  
    9.         <!--[CDATA[  
    10.             import com.demo.event.TestEvent;  
    11.   
    12.             protected function testforms1_CopyTextHandler(event:TestEvent):void  
    13.             {  
    14.                 this.t.text = event.message;  
    15.             }  
    16.   
    17.         ]]-->  
    18.     </fx:Script>  
    19.     <fx:Declarations>  
    20.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->  
    21.     </fx:Declarations>  
    22.     <components:testForms x="23" y="28" CopyText="testforms1_CopyTextHandler(event)">  
    23.     </components:testForms>  
    24.     <s:TextInput x="440" y="28" width="227" height="184" id="t"/>  
    25. </s:Application>  
  • 相关阅读:
    【数学】多项式取 ln
    【数学】多项式求逆
    【模拟 + 栈】AcWing 151. 表达式计算4
    Unity3D开发入门教程(一)——搭建开发环境
    Unity3D开发入门教程(三)——添加启动脚本
    Unity3D开发入门教程(二)—— Lua入门
    Unity3D开发入门教程(四)——用Lua实现组件
    哈希表
    邻接表
    并查集
  • 原文地址:https://www.cnblogs.com/niceofday/p/5318649.html
Copyright © 2011-2022 走看看