沙漠中的魚

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

          手動觸發事件

          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>

           

          轉載:http://livedocs.adobe.com/flex/3/html/events_07.html#220526

          主站蜘蛛池模板: 江孜县| 瓦房店市| 奉节县| 玉环县| 明星| 龙岩市| 察雅县| 宜州市| 礼泉县| 乌鲁木齐市| 景东| 北辰区| 闻喜县| 昌黎县| 太康县| 利辛县| 吉水县| 苏尼特左旗| 枝江市| 当涂县| 高雄市| 松桃| 汕头市| 和平区| 石屏县| 密山市| 米脂县| 五河县| 蓬安县| 顺义区| 东山县| 嵩明县| 崇明县| 盐源县| 大兴区| 迁安市| 泽州县| 临西县| 盖州市| 岢岚县| 长海县|