2 Replies Latest reply on Mar 6, 2014 11:37 PM by m_mfsi

    How can I change Label's text using ActionScript?

    havefreetime

      Hi~ everyone~

      I want change Label's text using ActionScript.

       

       

      Test.mxml

      ------------------------------------------------------------------------------------------ -------------------------------------------------------

      <?xml version="1.0" encoding="utf-8"?>

      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

            xmlns:s="library://ns.adobe.com/flex/spark"

            xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" initialize="init()">

      <fx:Script>

        <![CDATA[

         private function init():void{

          var btn:BtnScript = new BtnScript();

          this.addElement(btn);

         }

        ]]>

      </fx:Script>

       

      <fx:Declarations>

        <!-- Place non-visual elements (e.g., services, value objects) here -->

      </fx:Declarations>

      <s:Label x="182" y="149" text="Label" id="lbl"/>

      </s:Application>

      ------------------------------------------------------------------------------------------ -------------------------------------------------------

       

       

      BtnScript.as

      ------------------------------------------------------------------------------------------ -------------------------------------------------------

      package
      {
      import flash.events.MouseEvent;

      import mx.controls.Alert;

      import spark.components.Button;

      public class BtnScript extends Button
      {
        public function BtnScript()
        {
         super();
         this.addEventListener(MouseEvent.CLICK, eventHandler);
        }
       
        private function eventHandler(event:MouseEvent):void{

                // lbl.text = "changed text";
             // HOW CAN I ACCESS LABEL IN MXML FILE?

        }
      }
      }

       

      I'm about to learn flex. Please help me.

       

      Thanks in advanced.

        • 1. Re: How can I change Label's text using ActionScript?
          CleanCoder Level 2

          Add your event listener to "btn" from the MXML file and create the event handler in the MXML file, not in your BtnScript class. In the eventHandler you create in MXML you will be able to access "lbl";

          1 person found this helpful
          • 2. Re: How can I change Label's text using ActionScript?
            m_mfsi

            You could achive this very easily(but this shouldn't be used in production code as it'll be difficult to manage as the project grows) by defining a static data member varibale of DataType Label something like below, (modified/added code in BOLD)

             

            Test.mxml

            -------------------------------------------------------------------- --------------------------------------------------------------------

            <?xml version="1.0" encoding="utf-8"?>

            <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                  xmlns:s="library://ns.adobe.com/flex/spark"

                  xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" initialize="init()"

                  creationComplete="creationCompleteHandler(event)">

            <fx:Script>

              <![CDATA[

               private function init():void{

                 var btn:BtnScript = new BtnScript();

                 this.addElement(btn);

               }

               private function creationCompleteHandler(event:FlexEvent):void{

                 Model.someLabel = lbl;

               }

              ]]>

            </fx:Script>

             

            <fx:Declarations>

              <!-- Place non-visual elements (e.g., services, value objects) here -->

            </fx:Declarations>

            <s:Label x="182" y="149" text="Label" id="lbl"/>

            </s:Application>

            -------------------------------------------------------------------- --------------------------------------------------------------------

             

             

            BtnScript.as

            -------------------------------------------------------------------- --------------------------------------------------------------------

            package
            {
              import flash.events.MouseEvent;

              import mx.controls.Alert;

              import spark.components.Button;

              public class BtnScript extends Button
              {
                public function BtnScript()
                {
                  super();
                  this.addEventListener(MouseEvent.CLICK, eventHandler);
                }
             
                private function eventHandler(event:MouseEvent):void{

                      // lbl.text = "changed text";

                  // Now use Model.someLabel which is basically 'lbl'.

                  Model.someLabel.text = "changed text";  //Mission Accomplished....

                }
              }

            }

            ------------------------------------------------------------------ ---------------------------------------------------------------------



            Model.as

            -------------------------------------------------------------- ---------------------------------------------------------------

            package
            {

              public class Model
              {

                public static someLabel:Label;

              }

            }

            -------------------------------------------------------------- ---------------------------------------------------------------


            Note: This approach is just to give you an idea but instead should use the MVC pattern.