使用ActionScript的单击事件示例

<?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" applicationComplete="init()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
protected function init():void
{
test3.addEventListener(MouseEvent.CLICK,onClick)
}
protected function onClick(event:Event):void
{
Alert.show(event.target.label+"clicked","时间测试");
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Button id="test3" x="493" y="62" width="112" height="44" label="按钮"/>
</s:Application>

绑定事件

无论何时创建对某个变量的绑定,都会注册一个事件监听器,监听器会在变量被修改时做出响应。

ActionScript依赖ChangeWatcher类实现数据绑定

<?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"
applicationComplete="init()">
<fx:Script>
<![CDATA[
import mx.binding.utils.ChangeWatcher;
import mx.events.FlexEvent;
import mx.events.PropertyChangeEvent; protected var _watcher:ChangeWatcher; protected function init():void
{
toggleWatch();
}
protected function toggleWatch():void
{
if(_watcher&&_watcher.isWatching())//确定监视器活跃
{
_watcher.unwatch();//移除监视器的变量
test3.label="Watch";
}
else
{
_watcher=ChangeWatcher.watch(input,"text",onChange);
test3.label="Stop Watching!";
}
}
protected function onChange(event:Event):void
{
label1.text=input.text;
} ]]>
</fx:Script>
<s:Button id="test3" x="338" y="176" width="112"
height="44" label="Watch Text" click="toggleWatch()"/>
<s:Label id="label1" x="510" y="176" width="176" height="53" />
<s:TextInput id="input" x="510" y="239" width="176" height="53" text="start text"/>
</s:Application>

我们刚刚看到的是unwatch()方法的用法,该方法可以取消监视变量的行为。在使用事件监听器的情况下,同样可以做到这一点。如果事件监听器是在运行时通过ActionScript添加的,就可以使用removeEventListener()方法移除。Unwatch()方法只是使用removeEventListener停止事件的分派。

看如何移除事件监听器:

<?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">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.events.DragEvent;
protected function toggleListener():void
{
if(box.hasEventListener(MouseEvent.CLICK))
{
log("Listeners removeed");
//移除事件侦听器
box.removeEventListener(MouseEvent.MOUSE_OVER,onEvent);
box.removeEventListener(MouseEvent.MOUSE_OUT,onEvent);
box.removeEventListener(MouseEvent.MOUSE_MOVE,onEvent);
box.removeEventListener(MouseEvent.CLICK,onEvent);
}
else
{
log("Listeners added")
//添加事件侦听器
box.addEventListener(MouseEvent.CLICK,onEvent);
box.addEventListener(MouseEvent.MOUSE_MOVE,onEvent);
box.addEventListener(MouseEvent.MOUSE_OUT,onEvent);
box.addEventListener(MouseEvent.MOUSE_OVER,onEvent);
}
}
protected function log(text:String):void//添加新的String用于显示
{
logField.text=text+"\n"+logField.text;
}
protected function onEvent(event:Event):void
{
log("Event triggered:"+event.type);
} ]]>
</fx:Script>
<s:Button label="Toggle Listener" click=" toggleListener()"/>
<s:Group id="box">
<s:Rect width="200" height="50">
<s:fill>
<s:SolidColor color="0x979797"/>
</s:fill>
</s:Rect>
</s:Group>
<s:TextArea id="logField" width="400" height="400"/>
</s:Application>

Flex 事件机制的更多相关文章

  1. Flex事件机制学习-自定义事件实现类间通信 .

    今天,学习Flex自定义事件,可以使两个类通信,定义一个Main类. public class Main extends Sprite     {            public function ...

  2. Flex中利用事件机制进行主程序与子窗体间参数传递

    在开发具有子窗体,或者itemrenderer的应用时,常常涉及到子窗体向父窗体传递参数或者从itemrenderer内的控件向外部的主程序传递参数的需求.这些都可以通过事件机制这一统一方法加以解决. ...

  3. AS3事件机制概述

    事件机制是AS3的核心功能之一,没有充分掌握事件机制的方方面面,就不能算是精通AS3语言. 1. AS3事件机制的主要成员 IEventDispatcher:事件派发对象接口,定义了添加.派发.移除. ...

  4. javaScript tips —— z-index 对事件机制的影响

    demo // DOM结构 class App extends React.Component { componentDidMount() { const div1 = document.getEle ...

  5. React 为什么要把事件挂载到 document 上 & 事件机制源码分析

    前言 我们都知道 React 组件绑定事件的本质是代理到 document 上,然而面试被问到,为什么要这么设计,有什么好处吗? 我知道肯定不会是因为虚拟 DOM 的原因,因为 Vue 的事件就能挂载 ...

  6. 【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)

    前言 这篇博客有点长,如果你是高手请您读一读,能对其中的一些误点提出来,以免我误人子弟,并且帮助我提高 如果你是javascript菜鸟,建议您好好读一读,真的理解下来会有不一样的收获 在下才疏学浅, ...

  7. tkinter事件机制

    一.tkinter.Event tkinter的事件机制跟js是一样的,也是只有一个Event类,这个类包罗万象,集成了键盘事件,鼠标事件,包含各种参数. 不像java swing那种强类型事件,sw ...

  8. [解惑]JavaScript事件机制

    群里童鞋问到关于事件传播的一个问题:“事件捕获的时候,阻止冒泡,事件到达目标之后,还会冒泡吗?”. 初学 JS 的童鞋经常会有诸多疑问,我在很多 QQ 群也混了好几年了,耳濡目染也也收获了不少,以后会 ...

  9. Atitit  数据库的事件机制--触发器与定时任务attilax总结

    Atitit  数据库的事件机制--触发器与定时任务attilax总结 1.1. 事件机制的图谱1 2. 触发器的类型2 3. 实现原理 After触发器 Vs Instead Of触发器2 3.1. ...

随机推荐

  1. [转载]VMWare网络连接透析

    http://blog.csdn.net/struggleyb/article/details/1102214 以前在学校,VMWare里面的Gentoo Linux是采用network bridge ...

  2. ByteBuffer常用方法详解

    原文  http://blog.csdn.net/u012345283/article/details/38357851 缓冲区(Buffer)就是在内存中预留指定大小的存储空间用来对输入/输出(I/ ...

  3. centos 安装mysql密码修改后还是不能连接的原因

    centos 上安装mysql密码修改后还是不能连接出现错误:ERROR 1142 (42000): SELECT command denied to user ''@'localhost' for ...

  4. html5 canvas 运行起来绝对让你震撼!

    从一个大神那看到的,拷贝过来跟大家分享下! html <canvas></canvas> *{margin:0;padding:0;}body{background:#222; ...

  5. 制作OB图的时候,OB玩家进入后就退出的问题

    开始怀疑是 OB玩家没有建筑 所以强行退出了,有朋友说那是因为有无效的触发造成的 我没有测试过 最后解决是给OB玩家在地图中加上建筑 更新 最后测试,把OB玩家放到一个组里 开局KILL掉这个组的建筑 ...

  6. DjangoORM一对多&多对多操作

    简要说明 通过操作对象的方式操作数据库 详细步骤 models.py的结构是: 如果models.py中外键定义没有写 related_name='student_teacher', 可以直接用 st ...

  7. this .运算符 和 [] 运算符

    首先看这个  这两个运行结果是不一样的 前两个是3  后面是10 var length = 10; var arr = [function(){console.log(this.length);},2 ...

  8. Developing your first FNC custom control

    Friday, May 13, 2016 Some weeks ago, we released the TMS FNC UI Pack, a set of Framework Neutral Com ...

  9. iOS6与iOS7屏幕适配技巧

    一.没有包装任何 导航控制器 或者 UITabBarController 1.控制器的view是UIScrollView\UITableView\UICollectionView时(控制器是UITab ...

  10. swith

    “开关”(Switch)有时也被划分为一种“选择语句”.根据一个整数表达式的值,switch语句可从一系列代码选出一段执行.它的格式如下: switch(整数选择因子) { case 整数值1 : 语 ...