在您的focusOut處理程序中,您必須存儲將獲得焦點的對象。這是因爲當您顯示警報時,此警報窗口現在已成爲焦點,因此這些信息已消失。有關如何正確執行此操作,請參閱以下示例。我建議你建立一個做所有的工作適合你自己的自定義組件...
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.managers.FocusManager;
protected var previousValue:String;
protected var nextValue:String;
protected var focusInObject:InteractiveObject;
protected function isNumber(value:String):Boolean
{
// do your check
return true;
}
protected function textInput_focusOutHandler(event:FocusEvent):void
{
var value:String = TextInput(event.currentTarget).text;
if (!isNumber(value) || value == previousValue)
return;
nextValue = value;
focusInObject = event.relatedObject;
Alert.show("Are you sure you want to enter that number?", "", Alert.YES | Alert.NO, this, alertCloseHandler);
}
protected function alertCloseHandler(event:CloseEvent):void
{
if (event.detail == Alert.NO)
return;
previousValue = nextValue;
if (focusInObject)
systemManager.stage.focus = focusInObject;
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:TextInput focusOut="textInput_focusOutHandler(event)"/>
<s:CheckBox label="Foo"/>
<s:Button label="Bar"/>
</s:Application>
我已經包括一個小的優化。如果用戶按是,應用程序會記住該值。如果您獲得另一個focusOut事件並且該值自上次以來未更改,則不會顯示該警報。
謝謝格哈德!這正是我正在尋找的解決方案,但(大但),我不能讓它與我的代碼工作。我有許多TextInput控件,用戶將選中或單擊以輸入一系列數字。當我將另一個TextInput添加到代碼中時,然後在第一個輸入一個數字,然後單擊第二個數字,我遇到了focusOutHandler中的一個問題。雖然event.relatedObject看起來不錯,但focusInObject被設置爲null,看起來有一個問題,將IFocusManagerComponent接口附加到TextInput上。 – Carrie 2010-10-25 01:05:01
你知道一個解決方法嗎?我正在使用的代碼(編輯了一下,所以我可以運行它作爲一個Flex應用程序) – Carrie 2010-10-25 01:05:55
對不起,我不能粘貼在我的代碼BC它太長,但我剛剛添加第二個TextInput後 – Carrie 2010-10-25 01:07:42