1.基本的alert 使用
<mx:Button id="showAlertButton" click="showAlert(event)" label="Alert"/> <mx:Label id="displaySelectionLabel"/> <fx:Script> <![CDATA[ import mx.events.CloseEvent; import mx.controls.Alert; import mx.events.MenuEvent; private function showAlert(evt:MouseEvent):void { var alert:Alert = Alert.show("Button was clicked", "Alert Window Title", Alert.OK | Alert.CANCEL | Alert.NO | Alert.YES, this, onAlertClose); } private function onAlertClose(evt:CloseEvent):void { switch (evt.detail) { case Alert.OK: displaySelectionLabel.text = "OK Clicked"; break; case Alert.CANCEL: displaySelectionLabel.text = "CANCEL Clicked"; break; case Alert.NO: displaySelectionLabel.text = "NO Clicked"; break; case Alert.YES: displaySelectionLabel.text = "YES Clicked"; break; } } ]]> </fx:Script>
2.360旋转alert
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"> <mx:Style> @font-face { src: local("Verdana"); fontFamily: VerdanaEmbedded; } @font-face { src: local("Verdana"); fontFamily: VerdanaEmbedded; fontWeight: bold; } Alert { fontFamily: VerdanaEmbedded; creationCompleteEffect: myEffect; } </mx:Style> <mx:Script> <![CDATA[ import mx.controls.Alert; private var alert:Alert; private function button_click():void { alert = Alert.show("The quick brown fox jumped over the lazy dog", "Lorem Ipsum"); } ]]> </mx:Script> <mx:Sequence id="myEffect" > <mx:Parallel> <mx:Zoom /> <mx:Fade /> </mx:Parallel> <mx:Rotate /> </mx:Sequence> <mx:Button label="Launch Alert" click="button_click();" /> </mx:Application>
3.as 方式改变默认alert样式
<?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/halo" applicationComplete="init()"> <fx:Script> <![CDATA[ import mx.events.CloseEvent; import mx.controls.Alert; [Embed(source="1.png")] [Bindable] protected var _iconWarning:Class; protected function init():void{ var alert:Alert; Alert.buttonWidth = 150; Alert.okLabel = "Disneyland" Alert.yesLabel = "Kennedy Space Port"; Alert.noLabel = "Six Flags"; Alert.cancelLabel = "Marine World"; alert = Alert.show("Where do you want to go today?", "Destination", Alert.OK| Alert.YES | Alert.NO| Alert.CANCEL, this, onAlertClose, _iconWarning, Alert.YES); alert.height = 150; alert.width = 700; Alert.okLabel = "OK"; Alert.yesLabel = "Yes"; Alert.noLabel = "No"; Alert.cancelLabel = "Cancel"; } protected function onAlertClose(event:CloseEvent):void { var message:String = "Woohoo! Looks like we're going to "; switch (event.detail) { case Alert.YES : status.text = message + Alert.yesLabel; break;case Alert.NO : status.text = message + Alert.noLabel; break; case Alert.OK : status.text = message + Alert.okLabel; break; case Alert.CANCEL : status.text = message + Alert.cancelLabel; break; } } ]]> </fx:Script> <s:Label id="status" /> </s:Application>
效果图:
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2008/09/15/creating-a-custom-creation-complete-effect-on-a-flex-alert-control-redux/ --> <mx:Application name="Alert_creationCompleteEffect_test_3" xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.effects.easing.*; private function showAlert():void { var a:Alert = Alert.show("Nam vel nunc sed arcu fringilla fringilla. Cras dapibus nunc ut nisi.\\n" + "Nullam porttitor mi et mauris.\\n" + "Suspendisse hendrerit, turpis eu ornare suscipit, nulla sem tempus lorem,\\n" + "sit amet semper nibh mi cursus neque.\\n" + "Nulla vel purus. Sed a nulla. Quisque venenatis laoreet mi.", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit"); a.isPopUp = false; a.cacheAsBitmap = true; } ]]> </mx:Script> <mx:Style> @font-face { src: local("Verdana"); fontFamily: VerdanaEmbedded; } @font-face { src: local("Verdana"); fontFamily: VerdanaEmbedded; fontWeight: bold; } Alert { fontFamily: VerdanaEmbedded; creationCompleteEffect: alertCreationCompleteEffect; } </mx:Style> <mx:Parallel id="alertCreationCompleteEffect"> <mx:Fade duration="500" /> <mx:Move yFrom="0" easingFunction="Elastic.easeOut" duration="1000" /> </mx:Parallel> <mx:ApplicationControlBar dock="true"> <mx:Button id="button" label="Click me" click="showAlert();" /> </mx:ApplicationControlBar> </mx:Application>