http://www.flexrain.cn/flex/flex-alert.html
Flex里的Alert控件使用也很简单,不过我们也要了解常用的应用情况,它位于mx.controls.Alert,主要是show()方法,参数形式show(text, title, flags, parent, closeHandler, iconClass, defaultButtonFlag)
flags参数有些特别之处,就是我们可以用Alert.YES|Alert.No或者1|2|4来定义弹出框的按钮组合:
Yes按钮可以表示为:Alert.YES、 0×0001或者 1
No按钮可以表示为:Alert.No、0×0002 或者 2
OK按钮可以表示为:Alert.OK、0×0004 或者 4
Cancel按钮可以表示为:Alert.Cancel、0×0008 或者 8
closeHandler参数是当弹出窗关闭时回调的函数
iconClass参数是嵌入图标
defaultButtonFlag参数是默认的焦点按钮
Alert类还有其他一些常用的属性如:定义弹出按钮的高宽buttonWidth buttonHeight。定义四种按钮的标签名:yesLabel noLabel okLabel cancelLabel这些都能顾名思义到作用之处。
代码:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical" verticalAlign="middle"
- preloader="com.preload.PreLoad"
- backgroundColor="0x414141">
- <!--上面preload属性与下面外部CSS可以删除它-->
- <mx:Style source="yfskin/yflexskin.css"/>
- <mx:Style>
- Panel {
- drop-shadow-enabled: true;
- }
- </mx:Style>
- <mx:Script>
- <![CDATA[
- import mx.controls.Alert;
- import flash.events.Event;
- import mx.events.CloseEvent;
- private var _alert:Alert;
- private function showAlert():void
- {
- var text:String = "加油吧!Flex很好学的!"; //定义弹出框内容
- var title:String = "你好"; //定义弹出框标题
- _alert = Alert.show(text, title, Alert.YES | Alert.NO);
- }
- private function showAlert2(event:Event):void
- {
- var text:String = "注意点选按钮后,看Lable标签的返回显示";
- var title:String = "请选择";
- var defaultbtn:uint = 0x0002; //默认焦点按钮
- _alert = Alert.show(text, title, 1|2|4|8, this, closeFun, null, defaultbtn);
- }
- //关闭弹出框回调函数
- private function closeFun(event:CloseEvent):void
- {
- //trace (event.detail);
- switch (event.detail)
- {
- //1对应Yes按钮 2对应No按钮 4对应OK 8对应Canel
- case 1:
- status.text = "Yes";
- break;
- case 2:
- status.text = "No";
- break;
- case 4:
- status.text = "OK";
- break;
- case 8:
- status.text = "Cancel";
- break;
- default:
- break;
- }
- }
- private function SetAlertBtn():void
- {
- //宽度一定义就影响到其他铵钮宽度
- Alert.buttonWidth = 80;
- Alert.buttonHeight = 22; //默认高度
- //更改各按钮标签名
- Alert.yesLabel = "是";
- Alert.noLabel = "不";
- Alert.okLabel = "好";
- Alert.cancelLabel = "取消";
- status.text = "已经更改各按钮标签名了!";
- }
- private function defaultAlertBtn():void
- {
- //恢复按钮标签名
- Alert.yesLabel = "Yes";
- Alert.noLabel = "No";
- Alert.okLabel = "OK";
- Alert.cancelLabel = "Cancel";
- status.text = "现在各按钮标签名恢复了!";
- }
- ]]>
- </mx:Script>
- <mx:Label id="status" color="blue" />
- <mx:HBox>
- <mx:Button label="Alert1" click="showAlert();" />
- <mx:Button label="Alert2" click="showAlert2(event);" />
- <mx:Button label="SetAlertBtn" click="SetAlertBtn();" />
- <mx:Button label="defaultAlertBtn" click="defaultAlertBtn();" />
- </mx:HBox>
- </mx:Application>