Flex中如何利用键盘关闭(Esc)一个弹出窗口的例子

接下来的例子演示了Flex中如何利用键盘关闭(Esc)一个弹出窗口。

 

 

 

源文件:

main.mxml:

 

<? xml version="1.0" encoding="utf-8" ?>  
< mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml"  
        layout
="vertical"  
        verticalAlign
="top"  
        backgroundColor
="white" >  
  
    
< mx:Script >  
        
<![CDATA[  
            import mx.managers.PopUpManager; 
  
            private function button_click(evt:MouseEvent):void { 
                var popUpTitleWindow:PopUpTitleWindow = new PopUpTitleWindow(); 
                PopUpManager.addPopUp(popUpTitleWindow, this, true); 
            } 
        
]]>  
    
</ mx:Script >  
  
    
< mx:Button  id ="button"  
            label
="Launch Window"  
            click
="button_click(event);"   />  
  
</ mx:Application >

 

下面是PopUpTitleWindow.mxml的代码:

 

 

<? xml version="1.0" encoding="utf-8" ?>  
< mx:TitleWindow  xmlns:mx ="http://www.adobe.com/2006/mxml"  
        showCloseButton
="true"  
        styleName
="noPadding"  
        layout
="absolute"  
        width
="300"  
        height
="200"  
        creationComplete
="init();"  
        resize
="init();"  
        close
="titleWindow_close(event);"  
        keyDown
="titleWindow_keyDown(event);" >  
  
    
< mx:Style >  
        .noPadding { 
            paddingBottom: 0; 
            paddingTop: 0; 
            paddingLeft: 0; 
            paddingRight: 0; 
        } 
    
</ mx:Style >  
  
    
< mx:Script >  
        
<![CDATA[  
            import mx.core.IFlexDisplayObject; 
            import mx.events.CloseEvent; 
            import mx.managers.PopUpManager; 
  
            [Bindable] 
            public var source:String; 
  
            private function init():void { 
                PopUpManager.centerPopUp(this); 
                this.setFocus(); 
            } 
  
            private function titleWindow_close(evt:CloseEvent):void { 
                PopUpManager.removePopUp(evt.target as IFlexDisplayObject); 
            } 
  
            private function titleWindow_keyDown(evt:KeyboardEvent):void { 
                if (evt.charCode == Keyboard.ESCAPE) { 
                    this.dispatchEvent(new CloseEvent(CloseEvent.CLOSE)); 
                } 
            } 
        
]]>  
    
</ mx:Script >  
  
    
< mx:Label  id ="lbl"  
            text
="Press ESC to close this window."  
            fontWeight
="bold"  
            truncateToFit
="true"  
            horizontalCenter
="0"  
            verticalCenter
="0"   />  
  
    
< mx:ControlBar  horizontalAlign ="right"  width ="100%" >  
    
</ mx:ControlBar >  
  
</ mx:TitleWindow >

 

 

来自:http://blog.minidx.com/2008/08/06/1226.html

你可能感兴趣的:(Flex)