---
title: "Flex中Alert参数分析(转)"
author: "ytm"
date: "2010-12-30T11:46:19Z"
tags: []
categories: ["Web前端"]
slug: "flex-alert"
draft: false
lastmod: "2010-12-30T11:46:19Z"
id: 855673
url: "https://ytm.iteye.com/blog/855673"
---

Alert是我们常用的一个类，很简单，但是我想越基础的东西，如果有了很好的理解之后，也会对自己有所提高。

Alert中最重要的方法就是show（）了，因为其中的参数就可以定义弹出窗体的所有属性了，所以要对其充分认识，对于Alert总结了以下几点：

1、在mx.controls包中；

2、show（）中的参数：

     show(text:String = "", title:String = "", flags:uint = 0x4, parent:Sprite = null, closeHandler:Function = null, iconClass:Class = null, defaultButtonFlag:uint = 0x4):

     1） text：就是弹出窗口的提示信息；如：“确定删除你选择的照片吗？”等等

     2）title：就是弹出窗口的title，显示在左上角；如：“警告”；

     3）flags：这个参数是最重要的一个了，因为它决定了显示哪些按钮和模态的设定，可以看到它的类型是unit，所以是一个个的数字；Alert类中就有这样的常量，YES（0x0001），NO（0x0002），NONMODAL（0x8000），OK（0x0004），CANCEL（0x0008）等；如果要多个按钮，用“|”隔开；

    如：

Alert.show("Do you want to save your changes?", "Save Changes", Alert.NONMODAL|Alert.OK|Alert.NO, this,alertClickHandler,icon1);

由于flags是unit类型，直接写常数也是可以的，如：

Alert.show("Select a color:","Color Selection",1|2|8,this,null,icon1);

4）parent：弹出的Alert窗体，在其上居中；

5）closeHandler：关闭Alert窗体事件相应的函数；通过CloseEvent的detail属性可以获得用户选择的按钮；

如：

private function alertClickHandler(event:CloseEvent):void {

if (event.detail==Alert.YES)status.text="You answered Yes";else

status.text="You answered No";}

6）iconClass：用于设定弹出窗体的图标，利用Embed标签导入外部资源，然后可以用于这里；

7）defaultButtonFlag：用于设定默认按钮；

其他属性查帮助即可。

Alert.show()里面有多个属性，其中排在第三是flags，这个属性作用是在弹出的Alert提示框里面显示那一个或多个按钮，文档和书籍一般 只写该属性可以自由组合 Alert.OK, Alert.CANCEL,Alert.YES ,Alert.NO四个选项，并用“|”分隔，其实也可以用数字编号代替的，用数字编号更为简便，以下是编号对应的按钮组合表，一共有16个数字编号（其 实只有15种组合）。  
1- Alert.YES  
2- Alert.NO  
3- Alert.YES | Alert.NO  
4- Alert.OK  
5- Alert.OK | Alert.YES  
6- Alert.OK | Alert.NO  
7- Alert.OK | Alert.YES | Alert.NO  
8- Alert.CANCEL  
9- Alert.YES | Alert.CANCEL  
10- Alert.NO | Alert.CANCEL  
11- Alert.YES | Alert.NO | Alert.CANCEL  
12- Alert.OK | Alert.CANCEL  
13- Alert.OK | Alert.YES | Alert.CANCEL  
14- Alert.OK | Alert.NO | Alert.CANCEL  
15- Alert.OK | Alert.YES | Alert.NO | Alert.CANCEL  
16- Alert.OK （和4一样）

17开始返回到1重新按顺序循环………..而flags属性不填写的话一般默认值为Alert.OK，也就是4或16。

例子  
&lt;?xml version="1.0"?&gt;   
&lt;!-- Simple example to demonstrate the Alert control. --&gt;   
&lt;mx:Application xmlns:mx="[http://www.adobe.com/2006/mxml](http://www.adobe.com/2006/mxml)"&gt;   
   
    &lt;mx:Script&gt;   
        &lt;![CDATA[   
            import mx.controls.Alert;   
            import mx.events.CloseEvent;   
           
            // Event handler function uses a static method to show   
            // a pop-up window with the title, message, and requested buttons.           
            private function clickHandler(event:Event):void {   
                Alert.show("Do you want to save your changes?", "Save Changes", 3, this, alertClickHandler);   
            }   
           
            // Event handler function for displaying the selected Alert button.   
            private function alertClickHandler(event:CloseEvent):void {   
                if (event.detail==Alert.YES)   
                    status.text="You answered Yes";   
                else   
                    status.text="You answered No";   
            }   
   
            // Event handler function changes the default Button labels and sets the   
            // Button widths. If you later use an Alert with the default Buttons,    
            // you must reset these values.   
            private function secondClickHandler(event:Event):void {   
                Alert.buttonWidth = 100;   
                Alert.yesLabel = "Magenta";   
                Alert.noLabel = "Blue";   
                Alert.cancelLabel = "Green";   
   
                Alert.show("Select a color:","Color Selection",1|2|8,this);   
                   
                // Set the labels back to normal:   
                Alert.yesLabel = "Yes";   
                Alert.noLabel = "No";                   
            }   
        ]]&gt;   
    &lt;/mx:Script&gt;   
   
    &lt;mx:Panel title="Alert Control Example" width="75%" horizontalAlign="center" paddingTop="10"&gt;   
      &lt;mx:Text width="100%" color="blue" textAlign="center" fontSize="12"     
          text="点击下面按钮弹出一个简单的Alert窗口."/&gt;   
      &lt;mx:Button label="点击我" click="Alert.show('Hello World!', 'Message');"/&gt;   
   
      &lt;mx:Text width="100%" color="blue" textAlign="center" fontSize="12"     
          text="点击下面按钮弹出一个Alert窗口，并且捕获你按下的按钮."/&gt;   
      &lt;mx:Button label="点击我" click="clickHandler(event);"/&gt;   
      &lt;mx:Label id="status" fontWeight="bold"/&gt;   
   
      &lt;mx:Text width="100%" color="blue" textAlign="center" fontSize="12"     
          text="点击下面按钮弹出一个包括自定义标签按钮的Alert窗口."/&gt;   
      &lt;mx:Button label="点击我" click="secondClickHandler(event);"/&gt;   
    &lt;/mx:Panel&gt;   
   
&lt;/mx:Application&gt;