Flex4自定义事件类型Event的相关应用
2013-08-14 00:00
417 查看
基于松耦合的概念 自定义事件类型将取到很重要的作用
当您创建自己的自定义 Event 类时,必须覆盖继承的 Event.clone() 方法,以复制自定义类的属性。如果您未设置在事件子类中添加的所有属性,则当侦听器处理重新分派的事件时,这些属性将不会有正确的值。
自定义事件类 继承flash.events.Event类 下面看代码 其中 message 是自定义的属性, 下面要使用这个属性 来传递参数
[java]
view plain
copy
package com.demo.event
{
import flash.events.Event;
public class TestEvent extends Event
{
public static const EVENT_CLICK:String = "copy_text";
public var message:String;
public function TestEvent(type:String, message:String)
{
super(type);
this.message = message;
}
override public function clone():Event{
return new TestEvent(type,message);
}
}
}
接下来建立一个控件 来指派这个事件
注册事件 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
<?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="400" height="300">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Metadata>
[Event(name="CopyText",type="com.demo.event.TestEvent")]
</fx:Metadata>
<fx:Script>
<!--[CDATA[
import com.demo.event.TestEvent;
protected function button1_clickHandler(event:MouseEvent):void
{
dispatchEvent(new TestEvent("CopyText",tempText.text));
}
]]-->
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:TextInput x="10" y="10" height="107" width="260" id="tempText"/>
<s:Button x="14" y="124" label="Copy" click="button1_clickHandler(event)"/>
</s:Group>
最后将这个控件放到主程序中, 并使用了这个自定义事件
[c-sharp]
view plain
copy
<?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:components="com.demo.view.components.*">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<!--[CDATA[
import com.demo.event.TestEvent;
protected function testforms1_CopyTextHandler(event:TestEvent):void
{
this.t.text = event.message;
}
]]-->
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<components:testForms x="23" y="28" CopyText="testforms1_CopyTextHandler(event)">
</components:testForms>
<s:TextInput x="440" y="28" width="227" height="184" id="t"/>
</s:Application>
当您创建自己的自定义 Event 类时,必须覆盖继承的 Event.clone() 方法,以复制自定义类的属性。如果您未设置在事件子类中添加的所有属性,则当侦听器处理重新分派的事件时,这些属性将不会有正确的值。
自定义事件类 继承flash.events.Event类 下面看代码 其中 message 是自定义的属性, 下面要使用这个属性 来传递参数
[java]
view plain
copy
package com.demo.event
{
import flash.events.Event;
public class TestEvent extends Event
{
public static const EVENT_CLICK:String = "copy_text";
public var message:String;
public function TestEvent(type:String, message:String)
{
super(type);
this.message = message;
}
override public function clone():Event{
return new TestEvent(type,message);
}
}
}
接下来建立一个控件 来指派这个事件
注册事件 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
<?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="400" height="300">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Metadata>
[Event(name="CopyText",type="com.demo.event.TestEvent")]
</fx:Metadata>
<fx:Script>
<!--[CDATA[
import com.demo.event.TestEvent;
protected function button1_clickHandler(event:MouseEvent):void
{
dispatchEvent(new TestEvent("CopyText",tempText.text));
}
]]-->
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:TextInput x="10" y="10" height="107" width="260" id="tempText"/>
<s:Button x="14" y="124" label="Copy" click="button1_clickHandler(event)"/>
</s:Group>
最后将这个控件放到主程序中, 并使用了这个自定义事件
[c-sharp]
view plain
copy
<?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:components="com.demo.view.components.*">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<!--[CDATA[
import com.demo.event.TestEvent;
protected function testforms1_CopyTextHandler(event:TestEvent):void
{
this.t.text = event.message;
}
]]-->
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<components:testForms x="23" y="28" CopyText="testforms1_CopyTextHandler(event)">
</components:testForms>
<s:TextInput x="440" y="28" width="227" height="184" id="t"/>
</s:Application>
相关文章推荐
- Flex4自定义事件类型Event的相关应用
- Flex4自定义事件类型Event的相关应用
- Flex自定义事件类型,自定义事件,分发事件,以LoginEvent为例
- Axure RP Pro - 相关问题 - master主控文档的behavior行为是custom widget自定义部件时丢失raised event可触发事件
- 一个支持事件event的自定义EventTarget类型
- C++学习笔记(第七章 自定义数据类型 结构体应用 之三)
- FluorineFX框架下的.NET和FLEX当中数据转换(自定义类型)
- 描述相关WPF自定义路由事件知识
- 教你7步实现flex自定义Event及参数传递
- Flex中双击事件MouseEvent.DOUBLE_CLICK不起作用的解决办法
- 介绍嵌入式应用中存储器类型的相关选择技巧
- C# 委托(Delegate) 事件(Event)应用详解
- .Net: delegate(委托)、event(事件) 应用
- C# 自定义数据类型 数组应用 读取XML
- Struts 2中自定义类型转换器 TypeConverter 的应用
- event 事件 自定义滚动条 控制文字大小 透明度
- 发布首款支持Shift/Ctrl多选的国产日历控件,界面仿Twitter风格,支持中文日期类型,支持SImpleDateFormat格式,支持change等自定义事件,全中文注释和API,欢迎支持
- Symbian OS应用编程图形篇之事件(Event)
- 自定义控件(视图)2期笔记14:自定义视图之View事件分发 dispatchTouchEvent,onTouch,onTouchEvent,onClick逻辑顺序过程
- Flex的拖动事件 DragEvent DragManager