在as中监听自定义事件并处理事件的实例代码
网络编程 2021-07-05 11:32www.168986.cn编程入门
点击一张图片,响应事件。必须在AS中,去监听事件,并处理事件,狼蚁网站SEO优化是具体的实现思路及功能代码,感兴趣的朋友可以参考下哈
场景描述点击一张图片,响应事件。必须在AS中,去监听事件,并处理事件。
1 自定义了一个事件,如下
package bridge {
import flash.events.Event;
import mx.events.FlexEvent;
public class MyEvent extends Event {
public static const myclick:String="myclick";
public function MyEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
2 监听事件处理的AS类,如下
package handler
{
import bridge.MyEvent;
import flash.events.Event;
import mx.controls.Alert;
import skin.ImgHanderSkin;
import spark.ponents.BorderContainer;
import spark.ponents.Image;
public class ImgHander extends BorderContainer
{
[SkinPart(required="true")]
public var img:Image;// 打开按钮
public function ImgHander()
{
super();
this.setStyle("skinClass",ImgHanderSkin);
this.percentHeight=100;
this.percentWidth=100;
}
//初始化监听
override public function initialize():void{
super.initialize();
img.addEventListener(MyEvent.myclick,SetImgShouZhanUrl);
}
private function SetImgShouZhanUrl(event:Event):void {
Alert.show("Preview");
}
}
}
3 新建MXML外观,皮肤类,在这里发送自定义事件。(在assert文件夹下有一张图片哦:柯南.jpg)如下
<?xml version="1.0" encoding="utf-8"?>
<!--
ADOBE SYSTEMS INCORPORATED
Copyright 2008 Adobe Systems Incorporated
All Rights Reserved.
NOTICE: Adobe permits you to use, modify, and distribute this file
in aordance with the terms of the license agreement acpanying it.
-->
<!--- The default skin class for a Spark SkinnableContainer container.
@see spark.ponents.SkinnableContainer
@langversion 3.0
@playerversion Flash 10
@playerversion AIR 1.5
@productversion Flex 4
-->
<s:Skin xmlns:fx="http://ns.adobe./mxml/2009" xmlns:s="library://ns.adobe./flex/spark"
xmlns:fb="http://ns.adobe./flashbuilder/2009" alpha.disabled="0.5">
<fx:Metadata>[HostComponent("spark.ponents.BorderContainer")]</fx:Metadata>
<fx:Script fb:purpose="styling">
<![CDATA[
import bridge.MyEvent;
/
@private
/
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) : void
{
// Push backgroundColor and backgroundAlpha directly.
// Handle undefined backgroundColor by hiding the background object.
if (isNaN(getStyle("backgroundColor")))
{
background.visible = false;
}
else
{
background.visible = true;
bgFill.color = getStyle("backgroundColor");
bgFill.alpha = getStyle("backgroundAlpha");
}
super.updateDisplayList(unscaledWidth, unscaledHeight);
}
private function img_mouseOutHandler(event:MouseEvent):void{
// TODO Auto-generated method stub
var e:MyEvent= new MyEvent(MyEvent.myclick);
img.dispatchEvent(e);
}
]]>
</fx:Script>
<s:states>
<s:State name="normal" />
<s:State name="disabled" />
</s:states>
<!--- Defines the appearance of the SkinnableContainer class's background. -->
<s:Rect id="background" left="0" right="0" ="0" bottom="0">
<s:fill>
<!--- @private -->
<s:SolidColor id="bgFill" color="#FFFFFF"/>
</s:fill>
</s:Rect>
<!--
Note: setting the minimum size to 0 here so that changes to the host ponent's
size will not be thwarted by this skin part's minimum size. This is a promise,
more about it here: http://bugs.adobe./jira/browse/SDK-21143
-->
<!--- @copy spark.ponents.SkinnableContainer#contentGroup -->
<s:Group id="contentGroup" left="0" right="0" ="0" bottom="0" minWidth="0" minHeight="0">
<s:layout>
<s:BasicLayout/>
</s:layout>
<s:Image id="img" click="img_mouseOutHandler(event)" source="assert/柯南.jpg">
</s:Image>
</s:Group>
</s:Skin>
4 ,创建一个MXML应用程序里面,去调用,AS类。直接运行。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe./mxml/2009"
xmlns:s="library://ns.adobe./flex/spark"
xmlns:mx="library://ns.adobe./flex/mx"
minWidth="955" minHeight="600"
xmlns:handler="handler.">
<handler:ImgHander>
</handler:ImgHander>
</s:Application>
结束!
注意
1 发送自定义事件
var e:MyEvent= new MyEvent(MyEvent.myclick);
img.dispatchEvent(e);
2 接收并处理事件
img.addEventListener(MyEvent.myclick,SetImgShouZhanUrl);
1 自定义了一个事件,如下
代码如下:
package bridge {
import flash.events.Event;
import mx.events.FlexEvent;
public class MyEvent extends Event {
public static const myclick:String="myclick";
public function MyEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
2 监听事件处理的AS类,如下
代码如下:
package handler
{
import bridge.MyEvent;
import flash.events.Event;
import mx.controls.Alert;
import skin.ImgHanderSkin;
import spark.ponents.BorderContainer;
import spark.ponents.Image;
public class ImgHander extends BorderContainer
{
[SkinPart(required="true")]
public var img:Image;// 打开按钮
public function ImgHander()
{
super();
this.setStyle("skinClass",ImgHanderSkin);
this.percentHeight=100;
this.percentWidth=100;
}
//初始化监听
override public function initialize():void{
super.initialize();
img.addEventListener(MyEvent.myclick,SetImgShouZhanUrl);
}
private function SetImgShouZhanUrl(event:Event):void {
Alert.show("Preview");
}
}
}
3 新建MXML外观,皮肤类,在这里发送自定义事件。(在assert文件夹下有一张图片哦:柯南.jpg)如下
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!--
ADOBE SYSTEMS INCORPORATED
Copyright 2008 Adobe Systems Incorporated
All Rights Reserved.
NOTICE: Adobe permits you to use, modify, and distribute this file
in aordance with the terms of the license agreement acpanying it.
-->
<!--- The default skin class for a Spark SkinnableContainer container.
@see spark.ponents.SkinnableContainer
@langversion 3.0
@playerversion Flash 10
@playerversion AIR 1.5
@productversion Flex 4
-->
<s:Skin xmlns:fx="http://ns.adobe./mxml/2009" xmlns:s="library://ns.adobe./flex/spark"
xmlns:fb="http://ns.adobe./flashbuilder/2009" alpha.disabled="0.5">
<fx:Metadata>[HostComponent("spark.ponents.BorderContainer")]</fx:Metadata>
<fx:Script fb:purpose="styling">
<![CDATA[
import bridge.MyEvent;
/
@private
/
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) : void
{
// Push backgroundColor and backgroundAlpha directly.
// Handle undefined backgroundColor by hiding the background object.
if (isNaN(getStyle("backgroundColor")))
{
background.visible = false;
}
else
{
background.visible = true;
bgFill.color = getStyle("backgroundColor");
bgFill.alpha = getStyle("backgroundAlpha");
}
super.updateDisplayList(unscaledWidth, unscaledHeight);
}
private function img_mouseOutHandler(event:MouseEvent):void{
// TODO Auto-generated method stub
var e:MyEvent= new MyEvent(MyEvent.myclick);
img.dispatchEvent(e);
}
]]>
</fx:Script>
<s:states>
<s:State name="normal" />
<s:State name="disabled" />
</s:states>
<!--- Defines the appearance of the SkinnableContainer class's background. -->
<s:Rect id="background" left="0" right="0" ="0" bottom="0">
<s:fill>
<!--- @private -->
<s:SolidColor id="bgFill" color="#FFFFFF"/>
</s:fill>
</s:Rect>
<!--
Note: setting the minimum size to 0 here so that changes to the host ponent's
size will not be thwarted by this skin part's minimum size. This is a promise,
more about it here: http://bugs.adobe./jira/browse/SDK-21143
-->
<!--- @copy spark.ponents.SkinnableContainer#contentGroup -->
<s:Group id="contentGroup" left="0" right="0" ="0" bottom="0" minWidth="0" minHeight="0">
<s:layout>
<s:BasicLayout/>
</s:layout>
<s:Image id="img" click="img_mouseOutHandler(event)" source="assert/柯南.jpg">
</s:Image>
</s:Group>
</s:Skin>
4 ,创建一个MXML应用程序里面,去调用,AS类。直接运行。
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe./mxml/2009"
xmlns:s="library://ns.adobe./flex/spark"
xmlns:mx="library://ns.adobe./flex/mx"
minWidth="955" minHeight="600"
xmlns:handler="handler.">
<handler:ImgHander>
</handler:ImgHander>
</s:Application>
结束!
注意
1 发送自定义事件
代码如下:
var e:MyEvent= new MyEvent(MyEvent.myclick);
img.dispatchEvent(e);
2 接收并处理事件
img.addEventListener(MyEvent.myclick,SetImgShouZhanUrl);
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程