本文章轉自:http://blessht.iteye.com/blog/1108623
在任何BS項目中,消息提示框都是非常常見的功能組件,flex在AIR的渲染下,消息提示框也是做得非常漂亮美觀。
Flex的消息提示框由mx.controls.Alert類負責創建,通常通過調用靜態方法show(即可實現提示框的創建):
- public static show (
- text:String, //消息提示內容
- title:String=null, //標題
- flags:uint=mx.controls.Alert.OK, //按鈕組合
- parent:Sprite=null, //Alert 控件的父對象
- clickListener:Function=null, //指定 click 事件的偵聽器
- iconClass:Class=null, //指定對話框中消息文本左側的圖標
- defaultButton:uint=mx.controls.Alert.OK //使用一個標誌參數的合法值指定默認按鈕。當用戶按下回車時,此按鈕就被選中,其默認值是 Alert.OK.
- )
public static show (
text:String, //消息提示內容
title:String=null, //標題
flags:uint=mx.controls.Alert.OK, //按鈕組合
parent:Sprite=null, //Alert 控件的父對象
clickListener:Function=null, //指定 click 事件的偵聽器
iconClass:Class=null, //指定對話框中消息文本左側的圖標
defaultButton:uint=mx.controls.Alert.OK //使用一個標誌參數的合法值指定默認按鈕。當用戶按下回車時,此按鈕就被選中,其默認值是 Alert.OK.
)
show方法內所有參數都是非必選的。
參數flags表示彈出框下面生成幾種按鈕,alert類提供了四個按鈕:是、否、確定和取消,由四個整數抽象表示:
- Alert.OK 4
- Alert.NO 2
- Alert.YES 1
- Alert.CANCEL 8
Alert.OK 4
Alert.NO 2
Alert.YES 1
Alert.CANCEL 8
具體使用方法詳見後面的代碼。
參數clickListener可實現點擊按鈕事件監聽,也就是說可以通過監聽來判斷用戶點擊的是哪個按鈕,從而根據不同選擇實現不同操作。
下面來看一個實例:在界面上有三個按鈕,每點擊一個按鈕彈出一個提示框。這個功能非常簡單,只需要給每個button綁定click事件即可:
- <fx:Script>
- <![CDATA[
- import mx.controls.Alert;
- import mx.events.CloseEvent;
- protected function button1_clickHandler(event:MouseEvent):void
- {
- var myAlert:Alert = Alert.show("顯示對話框...","提示");
- myAlert.height = 200; //高度
- myAlert.width = 200; //寬度
- }
- protected function button2_clickHandler(event:MouseEvent):void
- {
- Alert.show("你確定此操作嗎?","提示",Alert.OK|Alert.CANCEL|Alert.YES|Alert.NO,this,handler);
- }
- protected function button3_clickHandler(event:MouseEvent):void
- {
- Alert.yesLabel = "喲系yes";
- Alert.noLabel = "呀滅no";
- Alert.cancelLabel = "哦cancel";
- var myAlert:Alert = Alert.show("選擇操作...","提示",1|2,this,handler);
- }
- private function handler(e:CloseEvent):void{
- //顯示事件選擇的值
- Alert.show(e.detail.toString());
- }
- ]]>
- </fx:Script>
- <fx:Declarations>
- <!-- 將非可視元素(例如服務、值對象)放在此處 -->
- </fx:Declarations>
- <s:Button label="按鈕1" click="button1_clickHandler(event)"/>
- <s:Button label="按鈕2" click="button2_clickHandler(event)"/>
- <s:Button label="按鈕3" click="button3_clickHandler(event)"/>
需要注意的是Alert.yesLabel、Alert.noLabel、Alert.cancelLabel等等set方法是全局的,如果相應屬性值改變,則其它Alert對象也會跟着改變。
最後看下運行效果: