从动作脚本访问mxml中的元素

时间:2013-02-27 12:02:03

标签: actionscript-3 flex mxml flex-spark

我有一个mxml应用程序,如下所示。如何从actionscript文件更改标签文本?

<?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" creationComplete="initApp()">
    <fx:Script>
        <![CDATA[
            public function initApp(){
                var p = new my_player("a");
            }
        ]]>
    </fx:Script>
    <s:Label x="700" y="409" text="Label" id="lble" width="131" height="41"/>
</s:Application>

my_player.as代码

package 
{
    import spark.components.Label;
    public class my_player
    {
        public var lble:Label;
        public function my_player(a:String)
        {
            lble.text="hello";
        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用构造函数(另一种方式 - 绑定)将主应用程序中的标签注入到convey_player中

public function initApp(){
    var p = new my_player("a", lble);
}

public function my_player (a:String, targetLabel:Label)
{
    lble = targetLabel;
    lble.text="hello";
}

答案 1 :(得分:0)

你不需要第二节课:(

<?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" 
    creationComplete="initApp()">
    <fx:Script>
        <![CDATA[
        public function initApp(){

            // access your components by id
            lble.text  = "My new text for the label"
        }
        ]]>
    </fx:Script>
    <s:Label x="700" y="409" text="Label" id="lble" width="131" height="41"/>
</s:Application>