关闭

flex自定义无参数事件

471人阅读 评论(0) 收藏 举报

自定义事件必须注意几点:

(1)自定义事件通过继承Event类来实现。

(2)自定义事件不可以自己触发,只能有系统事件派发。

了解这两点后现在就可以开始自定义自己的事件了。

首先我们新建一个ActionScript Class,这个Class继承flash.events.Event。

package
{
	import flash.events.Event;
	
	public class MyEvent extends Event
	{
		public function MyEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
		{
			super(type, bubbles, cancelable);
		}
		
		override public function clone():Event
		{
			// TODO Auto Generated method stub
			return new MyEvent(type,bubbles,cancelable);
		}
		
	}
}

下面创建一个登录的组件(component),并添加和使用自定义事件

<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow 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="260" fontSize="22" fontWeight="bold" textAlign="center"
			   title="用户登录"
			   >
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<fx:Metadata>
		[Event(name="myevent", type="MyEvent")]
	</fx:Metadata>
	
	<s:Form id="loginForm">
		<s:FormItem label="用户名:">
			<s:TextInput id="username" textAlign="left"/>
		</s:FormItem>
		<s:FormItem label="密   码:">
			<s:TextInput id="passwd" textAlign="left" displayAsPassword="true"/>
		</s:FormItem>
	</s:Form>
	<s:Button id="registerBtn" x="56" y="160" label="重置"/>
	<s:Button id="loginBtn" x="274" y="160" label="登录" click="clickHandle(event)"/>
	
	<fx:Script>
		<![CDATA[
			private function clickHandle(event:MouseEvent):void {
				this.dispatchEvent(new MyEvent("myevent",true,false));
			}
		]]>
	</fx:Script>
</s:TitleWindow>
在主程序中引入刚才新建的component,并触发该
<?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:myLogin = "*"
			   >
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<myLogin:MyLogin id="myLogin" x="278" y="114" myevent="clickedHandle(event)">
		
	</myLogin:MyLogin>
	
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			private function clickedHandle(event:MyEvent):void {
				Alert.show("The type of this event is "+event.type+"\nThe currentTarget is "+event.currentTarget+  
					"\nThe target is "+event.target);  
			}
		]]>
	</fx:Script>
</s:Application>
效果如下:


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:92583次
    • 积分:1320
    • 等级:
    • 排名:千里之外
    • 原创:39篇
    • 转载:28篇
    • 译文:0篇
    • 评论:9条
    最新评论