/ Published in: MXML
Simple example of how to bind addEventListener with MXML ID's
Expand |
Embed | Plain Text
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="400" creationComplete="initApp()"> <!-- Events A. Event: is a way for one piece of an application to tell other pieces that something has happened B. Broadcast: Fancy term for announcing an event. Also known as dispatch C. Listener: Watches for the Broadcast D. Handler: Performs action based on the event Component LifeCycle Events A. Preinitialize B. Initialize C. creationComplete 1. Trigger a server request (Ex: Load an RSS Feed) 2. Load Flash Variables 3. Set up event listeners or other config data D. updateComplete --> <mx:Script> <![CDATA[ private function initApp():void { btn1.addEventListener(MouseEvent.CLICK, onBtnClickHandler, false, 0, true ); btn2.addEventListener(MouseEvent.CLICK, onBtnClickHandler, false, 0, true ); btn3.addEventListener(MouseEvent.CLICK, onBtnClickHandler, false, 0, true ); } private function onBtnClickHandler( e:MouseEvent ):void { trace( "onBtnClickHandler: " + e.currentTarget.id ); switch( e.currentTarget.id ){ case "btn1": bg.setStyle('backgroundColor', 0xFFFFFF); break; case "btn2": bg.setStyle( 'backgroundColor', 0x000000 ); break; case "btn3": bg.setStyle( 'backgroundColor', 0xFFFF00 ); break; } } ]]> </mx:Script> <mx:Panel id="panel" x="10" y="10" width="380" height="150" horizontalAlign="center" verticalAlign="middle"> <mx:HBox id="hbox" width="90%" height="90%"> <mx:Button id="btn1" label="Change White" height="100%" /> <mx:Button id="btn3" label="Change Green" height="100%" /> <mx:Button id="btn2" label="Change Black" height="100%" /> </mx:HBox> </mx:Panel> <mx:Canvas id="bg" width="90%" height="200" y="180" horizontalCenter="0" /> </mx:Application>
You need to login to post a comment.
