Air / Flex将变量与属性连接起来

时间:2010-04-08 14:28:36

标签: flex actionscript air

我在舞台上有三个文本框id =红色,蓝色,绿色与我的按键相同 汽车对象/阵列

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/halo" creationComplete="carsToBox()">
    <fx:Script>
        <![CDATA[
            public function carsToBox():void{

                var cars:Object={red:"300zx",blue:"Skyline",green:"Supra"};
                    for(var tempObj:String in cars)
                    {
                        tempObj.text= cars[tempObj];
                    }
            }       
        ]]>
    </fx:Script>

    <s:TextInput x="65" y="53" id="red"/>
    <s:TextInput x="64" y="88" id="blue"/>
    <s:TextInput x="64" y="118" id="green"/>
</s:WindowedApplication>

所以我认为“tempObj.text”等于red.text但是我不能用“.text”来粘贴“tempObj”有没有办法可以做到这一点?

1 个答案:

答案 0 :(得分:1)

基本上你的tempObj实际上是cars对象的属性名称。为了清晰起见,我已将其重命名为prop。由于汽车的属性名称与textInput控件的属性名称相同,因此您可以使用该名称:

<?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                           xmlns:s="library://ns.adobe.com/flex/spark" 
                           xmlns:mx="library://ns.adobe.com/flex/halo" creationComplete="carsToBox()">
        <fx:Script>
            <![CDATA[
                public function carsToBox():void
                {

                    var cars:Object={red:"300zx",blue:"Skyline",green:"Supra"};
                    for(var prop:String in cars)
                    {
                        this[prop].text = cars[prop]
                    }
                }       
            ]]>
        </fx:Script>

        <s:TextInput x="65" y="53" id="red"/>
        <s:TextInput x="64" y="88" id="blue"/>
        <s:TextInput x="64" y="118" id="green"/>
    </s:WindowedApplication>