沙漠中的魚

          欲上天堂,先下地獄
          posts - 0, comments - 56, trackbacks - 0, articles - 119
            BlogJava :: 首頁 ::  :: 聯(lián)系 :: 聚合  :: 管理

          手動觸發(fā)事件

          Posted on 2008-10-28 11:57 沙漠中的魚 閱讀(690) 評論(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>

           

          轉(zhuǎn)載:http://livedocs.adobe.com/flex/3/html/events_07.html#220526

          主站蜘蛛池模板: 彰化县| 雅安市| 邳州市| 区。| 桂阳县| 九龙坡区| 岗巴县| 皮山县| 黄梅县| 朝阳县| 张掖市| 六安市| 永新县| 墨江| 丹江口市| 武清区| 潞城市| 和龙市| 桃园市| 卓尼县| 徐州市| 长子县| 黔西| 元朗区| 桃园市| 湛江市| 城固县| 都匀市| 屯昌县| 宽甸| 利川市| 翁牛特旗| 保定市| 会东县| 阳西县| 长武县| 沂源县| 门源| 通辽市| 资中县| 承德县|