Copy link to clipboard
Copied
<?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:Script><![CDATA[
private function init():void {
button3.addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(event:MouseEvent):void {
if (event.target == button3) {
label.text += 'Buton 3 Clicked\n';}
}
]]></fx:Script>
<s:VGroup width="100%"><s:Button id="button3" label="Button 3"/>
<s:Label id="label"/>
</s:VGroup>
</s:Application>
The above code does not seem to work.
Copy link to clipboard
Copied
you have a couple of problems with your code... you dont call the init function anywhere, so the addeventlistener never gets initialized...
so you can either add init to the creation complete or initialize in the application tag
and also, when you do event.target, you have to do event.target.id == "button3"
id is a string...
the modifications are as follows...
<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" creationComplete="init();">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script><![CDATA[
private function init():void {
button3.addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(event:MouseEvent):void {
if (event.target.id == "button3") {
label.text += 'Buton 3 Clicked\n';
}
}
]]></fx:Script>
<s:VGroup width="100%">
<s:Button id="button3" label="Button 3"/>
<s:Label id="label"/>
</s:VGroup>
</s:Application>
Copy link to clipboard
Copied
The
creationComplete="init();"
is what I needed.
But I did not need:
event.target.id == "button3"
My code now works fine, thank you.