有许多按钮,当点击其中一个按钮时,如何更改其标签?

时间:2012-07-25 06:39:52

标签: flex events onclick

我正在用mxml编写一个非常简单的flex应用程序。我有很多按钮,当我点击其中一个时,我希望它的值变为World

我的代码是:

<?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">
    <fx:Declarations>
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            private function hello():void {
                this.label = "World!";
            }
        ]]>
    </fx:Script>

    <mx:HBox>
        <s:Button click="hello()" label="Hello" />
        <s:Button click="hello()" label="Hello" />
        <s:Button click="hello()" label="Hello" />
        <s:Button click="hello()" label="Hello" />
        <s:Button click="hello()" label="Hello" />
    </mx:HBox>

</s:Application>

哪个不正确,因为无法编译this.label = "World!"未找到this.label

如何让this引用我点击的按钮,或者如何实现它?

1 个答案:

答案 0 :(得分:1)

尝试以下代码,这可能会对您有所帮助: -

<?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">
    <fx:Declarations>
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            private function hello(event:MouseEvent):void {
                event.currentTarget.label = "World!";
            }
        ]]>
    </fx:Script>

    <mx:HBox>
        <s:Button click="hello(event)" label="Hello" />
        <s:Button click="hello(event)" label="Hello" />
        <s:Button click="hello(event)" label="Hello" />
        <s:Button click="hello(event)" label="Hello" />
        <s:Button click="hello(event)" label="Hello" />
    </mx:HBox>

</s:Application>