java 手动触发事件_手动触发事件

You can manually dispatch events using a component instance's dispatchEvent() method. All components that extend UIComponent have this method. The method is inherited from the EventDispatcher class, which UIComponent extends.

The syntax for the dispatchEvent() method is as follows:

objectInstance.dispatchEvent(event:Event):Boolean

When dispatching an event, you must create a new Event object. The syntax for the Event object constructor is as follows:

Event(event_type:String, bubbles:Boolean, cancelable:Boolean)

The event_type parameter is the type property of the Event object. The bubbles and cancelable parameters are optional and both default to false. For information on bubbling and capturing, see Event propagation.

You can use the dispatchEvent() method to dispatch any event you want, not just a custom event. You can dispatch a Button control's click event, even though the user did not click a Button control, as in the following example:

4f1150b881333f12a311ae9ef34da474.png<?xml  version="1.0"?>4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png        private function createListener(e:Event):void {

4f1150b881333f12a311ae9ef34da474.png            b1.addEventListener(MouseEvent.MOUSE_OVER, myEventHandler);

4f1150b881333f12a311ae9ef34da474.png            b1.addEventListener(MouseEvent.CLICK, myClickHandler);

4f1150b881333f12a311ae9ef34da474.png        }

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png        private function myEventHandler(e:Event):void {

4f1150b881333f12a311ae9ef34da474.png            var result:Boolean = b1.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));           

4f1150b881333f12a311ae9ef34da474.png        }

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png        private function myClickHandler(e:Event):void {

4f1150b881333f12a311ae9ef34da474.png            Alert.show("The event dispatched by the MOUSE_OVER was of type '" + e.type + "'.");

4f1150b881333f12a311ae9ef34da474.png        }

4f1150b881333f12a311ae9ef34da474.png]]>4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

You can also manually dispatch an event in an MXML tag. In the following example, moving the mouse pointer over the button triggers the button's click event:

4f1150b881333f12a311ae9ef34da474.png<?xml  version="1.0"?>4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png        private function createListener(e:Event):void {

4f1150b881333f12a311ae9ef34da474.png            b1.addEventListener(MouseEvent.CLICK, myClickHandler);

4f1150b881333f12a311ae9ef34da474.png        }

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png        private function myClickHandler(e:Event):void {

4f1150b881333f12a311ae9ef34da474.png            Alert.show("The event dispatched by the MOUSE_OVER was of type '" + e.type + "'.");

4f1150b881333f12a311ae9ef34da474.png        }

4f1150b881333f12a311ae9ef34da474.png]]>4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png        label="Click Me"4f1150b881333f12a311ae9ef34da474.png        mouseOver="b1.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));"4f1150b881333f12a311ae9ef34da474.png/>4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值