4 Replies Latest reply on Sep 9, 2010 7:05 AM by Arun Ganesh

    how to add event listener?

    pad_birk

      i want to add event listener when i click on a button in mxml, the event listener should be added in the action script file, how to do this?

       

      can anyone help? urgent!!!

        • 1. Re: how to add event listener?
          BhaskerChari Level 4

          Hi Lakshmi,

           

          Here is the simple example of how to add an eventListener in ActionScript...

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onCreationComplete()">
              <mx:Script>
                  <![CDATA[
                   import mx.controls.Alert;

                      private function onCreationComplete():void
                      {
                       myButton.addEventListener(MouseEvent.CLICK, onButtonClick);
                      }
                      private function onButtonClick(event:MouseEvent):void
                      {
                       Alert.show("Button is Clicked");
                      }
                  ]]>
              </mx:Script>

          <mx:Button id="myButton" label="Click ME"/>
          </mx:Application>

           

           

          Thanks,

          Bhasker

          • 2. Re: how to add event listener?
            pad_birk Level 1

            Hi,

             

            I would like to know how to refer when the script and the mxml are not in the same file.

             

            here in this example, the script and the myButton are in the same file.

             

            Pls help

             

            Regards,

            Lakshmi

            • 3. Re: how to add event listener?
              BhaskerChari Level 4

              Hi Lakshmi,

               

              You can do this just put all the script in the mxml block in seperate AS file as shown below... Observe that I have included an AS file named Script.as and removed the script block form mxml and moved to this Script.as file. Place the Script.as file in your src folder ....

               

              // Main mxml file....

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onCreationComplete()">
                  <mx:Script source="Script.as"/>
                  
              <mx:TextArea id="textArea" width="300" height="100" />
              <mx:Button id="myButton" label="Click ME"/>
              </mx:Application>

               

              //Script.as file

              import mx.controls.Alert;

                          private function onCreationComplete():void
                          {
                           myButton.addEventListener(MouseEvent.CLICK, onButtonClick);
                          }
                          private function onButtonClick(event:MouseEvent):void
                          {
                           Alert.show("Button is Clicked");
                          }

               

               

              Thanks,

              Bhasker

              • 4. Re: how to add event listener?
                Arun Ganesh Level 3

                For that u need to create custom event.

                 

                Check this link,

                http://livedocs.adobe.com/flex/3/html/help.html?content=createevents_1.html