2011-10-26 77 views
0

這裏是一個功能例如:警報防止事件的內容與實際改變焦點

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:Script> 
     <![CDATA[ 
      import mx.controls.Alert; 
      public function go():void{    
       Alert.show("The focus will return to txtOne. Look: "); 
      } 
     ]]> 
    </mx:Script> 
    <mx:VBox> 
     <mx:TextInput id="txtOne" text="1" focusOut="go()"/> 
     <mx:TextInput id="txtTwo" text="2"/>  
    </mx:VBox> 
</mx:Application> 

當您更改txtOne到txtTwo,該警報顯示,然後按確定後,焦點將返回到txtOne。我不希望這種情況發生。如何解決這個問題?

回答

0

Alert.show有關閉回調參數,所以你會知道,當它關閉並將焦點設置到別的東西。 更新:你應該有標誌,說明,側重了可以處理事件:

private var needAlert:Boolean = true; 

public function go():void 
{ 
    if (needAlert) 
    { 
     Alert.show("The focus will return to txtOne. Look: ", 
      "", 0, null, myCloseHandler); 
     needAlert = false; 
    } 
} 

private function myCloseHandler(event:CloseEvent):void 
{ 
    this.setFocus(); 
    needAlert = true; 
} 
+0

有趣的是,這會導致無限循環。當按下OK時,焦點會變回txtOne,所以稱爲我的closeHandler函數。當我使用該功能改變焦點時,焦點輸出會再次被調用,在那裏你可以看到它的進展情況。 –

+0

檢查更新的答案 – moropus

0

嘗試,這也保證了重點飛到哪裏,用戶希望它太

<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:mx="library://ns.adobe.com/flex/mx"> 
<fx:Script> 
    <![CDATA[ 
     import mx.controls.Alert; 
     import mx.events.CloseEvent; 
     import mx.managers.IFocusManagerComponent; 
     private var ignoreFocusChange:Boolean = false; 

     private var focusTargetOnReturn: IFocusManagerComponent; 

     public function go():void 
     { 

      if(!ignoreFocusChange){ 
       focusTargetOnReturn = focusManager.getFocus(); 

       Alert.show("The focus will not return to txtOne anymore. Look: ", 
        "", 0, null, myCloseHandler); 
      } 
     } 

     private function myCloseHandler(event:CloseEvent):void 
     {    
      ignoreFocusChange = true; 
      focusManager.setFocus(focusTargetOnReturn); 
      ignoreFocusChange = false; 
     } 
    ]]> 
</fx:Script> 
<mx:VBox> 
    <mx:TextInput id="txtOne" text="1" focusOut="go()"/> 
    <mx:TextInput id="txtTwo" text="2"/>  
</mx:VBox>