May 02

接下来的例子演示了Flex中如何通过监听close事件,在静态Alert.show()事件中指定closeHandler的参数,来实现检测Alert控件(对话框)是否关闭。

让我们先来看一下Demo(可以右键View Source或点击这里察看源代码


下面是完整代码(或点击这里查看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="middle"
  5.         backgroundColor="white"
  6.         creationComplete="init();">
  7.  
  8.     <mx:Script>
  9.         <![CDATA[
  10.             import mx.events.CloseEvent;
  11.             import mx.controls.Alert;
  12.  
  13.             private var alert:Alert;
  14.  
  15.             private function init():void {
  16.                 Alert.buttonWidth = 100;
  17.                 Alert.yesLabel += " (" + Alert.YES + ")";
  18.                 Alert.noLabel += " (" + Alert.NO + ")";
  19.                 Alert.okLabel += " (" + Alert.OK + ")";
  20.                 Alert.cancelLabel += " (" + Alert.CANCEL + ")";
  21.             }
  22.  
  23.             private function showAlert():void {
  24.                 var flags:uint = 0;
  25.                 if (yesCheckBox.selected) flags += Alert.YES;
  26.                 if (noCheckBox.selected) flags += Alert.NO;
  27.                 if (okCheckBox.selected) flags += Alert.OK;
  28.                 if (cancelCheckBox.selected) flags += Alert.CANCEL;
  29.                 if (nonModalCheckBox.selected) flags += Alert.NONMODAL;
  30.                 alert = Alert.show("The quick brown fox jumped over the lazy dog.",
  31.                                     "title",
  32.                                     flags,
  33.                                     null,
  34.                                     alert_close);
  35.             }
  36.  
  37.             private function alert_close(evt:CloseEvent):void {
  38.                 arrColl.addItem(evt);
  39.             }
  40.         ]]>
  41.     </mx:Script>
  42.  
  43.     <mx:ArrayCollection id="arrColl" />
  44.  
  45.     <mx:ApplicationControlBar dock="true">
  46.         <mx:Button label="Show alert"
  47.                 click="showAlert();" />
  48.     </mx:ApplicationControlBar>
  49.  
  50.     <mx:DataGrid id="dataGrid"
  51.             dataProvider="{arrColl}"
  52.             itemRenderer="mx.controls.Label"
  53.             width="100%"
  54.             height="100%" />
  55.  
  56.     <mx:HBox width="100%">
  57.         <mx:CheckBox id="yesCheckBox"
  58.                 label="Alert.YES"
  59.                 width="20%" />
  60.         <mx:CheckBox id="noCheckBox"
  61.                 label="Alert.NO"
  62.                 width="20%" />
  63.         <mx:CheckBox id="okCheckBox"
  64.                 label="Alert.OK"
  65.                 width="20%" />
  66.         <mx:CheckBox id="cancelCheckBox"
  67.                 label="Alert.CANCEL"
  68.                 width="20%" />
  69.         <mx:CheckBox id="nonModalCheckBox"
  70.                 label="Alert.NONMODAL"
  71.                 width="20%" />
  72.     </mx:HBox>
  73.  
  74. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:minidxer

written by Minidxer  |  tags: , , ,

Related Post

Leave a Reply