Flex2 event - Notes

Custom-defined components


// MyButton.as
package tonyas.helloWorld
{
import mx.controls.Button;

public class MyButton extends Button
{
public function MyButton()
{
super();
this.label = "MyButton";
}

}
}

// HelloWorld.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:my="tonyas.helloWorld.*" layout="vertical">
<mx:HBox width="400">
<my:MyButton id="myButton" />
</mx:HBox>
</mx:Application>

Event

The Flex event model is based on the Document Object Model (DOM) Level 3 Events Model.

Event propagation: 1. capturing phase, 2. targeting phase, 3. bubbling phase. 说白了, 就是一个event发生后, 会从发出event的object (即target) 所属的根结点开始检测listener并执行listener, 一直到target的父结点为capturing phase; 遍历到target自身为targeting phase; 之后又与capturing phase反方向遍历到根结点为bubbling phase.

并不是所有的event都会经历这三个阶段. 有些event就只有targeting phase. Only DisplayObject objects (visual objects such as containers and controls) can have a capturing phase and a bubbling phase in addition to the targeting phase. 而event在什么阶段可以被listen可以在addEventListener中设定.

addEventListener声明为addEventListener(event_type:String, event_listener:Function, use_capture:Boolean=flase, priority:int=0, weakRef:Boolean=false)

而且The drawbacks are that you cannot set the useCapture or priority properties on the Event object and that you cannot remove the listener once you add it.

默认情况下, use_capture为false, 所以只有在bubble下会listen, 如果use_capture设为true, 则只有在capture时会listen.

所以If you want your event to traverse both the capturing and bubbling phases, you must call addEventListener() twice: once with use_capture set to true, and then again with use_capture set to false.

另外, An event only bubbles if its bubbles property is set to true. Events that can be bubbled include change, click, doubleClick, keyDown, keyUp, mouseDown, and mouseUp.

Stop propagation

During any phase, you can stop the traversal of the display list by calling one of the following methods on the Event object:
¦ stopPropagation()
¦ stopImmediatePropagation()

The two methods are nearly identical and differ only in whether the current node’s remaining event listeners are allowed to execute. The stopPropagation() method prevents the Event object from moving on to the next node, but only after any other event listeners on the current node are allowed to execute.

Event class 都是继承于flash.events.Event. 其子类有两类: mx.events.* are specific to Flex controls; flash.events.* aredefined by flash player. 一个event object 中包括the type of event and a reference to the dispatching control. flash.events.Event包含这些properties: bubbles : Boolean; cancelable : Boolean; currentTarget : Object; eventPhase : uint; target : Object; type : String.

对于event object, You can set properties of the Event object in ActionScript, but you cannot add new properties because the object is not dynamic.

When you handle a mouse event such as MouseEvent.CLICK by writing a listener on some component, the event.target property does not necessarily refer to that component; it is often a subcomponent, such as the Button control’s UITextField, that defines the label.

EventDispatcher class 都实现了接口flash.events.IEventDispatcher. 用户交互比如点击可以dispatch event, 也可以用AS实现manually dispatch event. 如下:


public var mev:MouseEvent = new MouseEvent(MouseEvent.CLICK,false,false);
mev.shiftKey = true;
myButton.dispatchEvent(mev);

或者 :


<mx:Button id="button1" label="Click Me" click="myDateChooser.dispatchEvent(new Event(Event.CHANGE))"/><mx:DateChooser id="myDateChooser"> 

You can only register an event listener with an object if that object dispatches the event.

Event listener or event handle可以是a function or class method.

注册listener时, 有如下两种方式, 第二种只可用于inline event.


myButton.addEventListener(MouseEvent.CLICK, myEventListener);
<mx:Button id="b1" label="Click Me" click="myEventListener()"/>

传递参数给event listener

定义Event listener时不一定带Event参数. 要传递event object给listener, 对于addEventListener没什么区别, 但对于inline event来说, 必须add the event keyword inside the MXML tag so that Flex explicitly passes it to the listener, 如下:


<mx:Button id="b1" label="Click Me" click="myEventListener(event)"/>

要传递额外的参数给event listener, 对于addEventListener是无法实现的, 因为不能传递多余的参数给它, 实际上这个功能完全有bypass的方案; 而对于inline event来说, 可以在mxml的tag中传递任意多个参数, 如:


<mx:Button id="b2" label="Down" click='runMove("down",event);'/>

Detecting the event phase

You can determine what phase you are in by using the Event object’s eventPhase property.

Priority

Flex calls event listeners in priority order, from highest to lowest. But even if you give a listener a higher priority than other listeners, there is no way to guarantee that the listener will finish executing before the next listener is called.

Keyboard event

用于全局地捕获键盘输入具体见Flex™ 2 Developer’s Guide (p108):

It is common for applications to respond to a key or series of keys and perform some action— for example, Control+q to quit the application. While Flash Player supports all the basic functionality of key combinations from the underlying operating system, it also lets you override or trap any key or combination of keys to perform a custom action.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值