0

我正在用 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我单击的按钮的引用,或者如何实现这个?

4

1 回答 1

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>
于 2012-07-25T07:31:35.037 回答