沙漠中的鱼

欲上天堂,先下地狱
posts - 0, comments - 56, trackbacks - 0, articles - 119
  BlogJava :: 首页 ::  :: 联系 :: 聚合  :: 管理

手动触发事件

Posted on 2008-10-28 11:57 沙漠中的鱼 阅读(673) 评论(0)  编辑  收藏 所属分类: Flex

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:

<?xml version="1.0"?>
<!-- events/DispatchEventExample.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="createListener(event);">
    
<mx:Script><![CDATA[
        import mx.controls.Alert;
        private function createListener(e:Event):void {
            b1.addEventListener(MouseEvent.MOUSE_OVER, myEventHandler);
            b1.addEventListener(MouseEvent.CLICK, myClickHandler);
        }

        private function myEventHandler(e:Event):void {
            var result:Boolean = b1.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));           
        }

        private function myClickHandler(e:Event):void {
            Alert.show("The event dispatched by the MOUSE_OVER was of type '" + e.type + "'.");
        }
    
]]></mx:Script>

    
<mx:Button id="b1" label="Click Me"/>

</mx:Application>

 

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:

 

<?xml version="1.0"?>
<!-- events/DispatchEventExampleInline.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="createListener(event);">
    
<mx:Script><![CDATA[
        import mx.controls.Alert;

        private function createListener(e:Event):void {
            b1.addEventListener(MouseEvent.CLICK, myClickHandler);
        }

        private function myClickHandler(e:Event):void {
            Alert.show("The event dispatched by the MOUSE_OVER was of type '" + e.type + "'.");
        }
    
]]></mx:Script>

    
<mx:Button id="b1" 
        label
="Click Me" 
        mouseOver
="b1.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));"
    
/>

</mx:Application>

 

转载:http://livedocs.adobe.com/flex/3/html/events_07.html#220526


只有注册用户登录后才能发表评论。


网站导航: