<?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"
creationComplete="_creationCompleteHandler(event)">
<s:layout>
<s:VerticalLayout />
</s:layout>
<fx:Script>
<![CDATA[
import flash.events.FocusEvent;
import mx.events.FlexEvent;
import spark.components.TextInput;
import spark.events.TextOperationEvent;
//---------------------------------------------------------------------
//
// constants
//
//---------------------------------------------------------------------
/**
* @private
* TextInput instances id prefix
*/
private const TEXT_INPUT_ID_PREFIX:String = "textInput";
//---------------------------------------------------------------------
//
// private methods
//
//---------------------------------------------------------------------
/**
* @private
*
* Create TextInput instances
*/
private function _createTextInputs():void
{
var count:uint = 5;
var textInput:TextInput;
for (var i:uint; i < count; i++)
{
textInput = new TextInput();
textInput.id = TEXT_INPUT_ID_PREFIX + i.toString();
// listen TextOperationEvent.CHANGE event
textInput.addEventListener(TextOperationEvent.CHANGE, _textInput_changeHandler);
// or listen FocusEvent.FOCUS_OUT
//textInput.addEventListener(FocusEvent.FOCUS_OUT, _textInput_focusOutHandler);
addElement(textInput);
}
}
/**
* @private
*
* Get TextInput instance index and value and pass in this values to "saving to database" method
*
* @param textInput TextInput instance
*/
private function _handleChangesInTextInput(textInput:TextInput):void
{
var textInputIndex:int = parseInt(textInput.id.replace(TEXT_INPUT_ID_PREFIX, ""), 10);
var textInputValue:String = textInput.text;
_saveTextInputDataToDatabase(textInputIndex, textInputValue);
}
/**
* @private
*
* Mockup method for saving to database
*
* @param textInputIndex TextInput instance index
* @param textInputValue TextInput instace value
*/
private function _saveTextInputDataToDatabase(textInputIndex:int, textInputValue:String):void
{
//TODO save data to database
}
//---------------------------------------------------------------------
//
// handlers
//
//---------------------------------------------------------------------
/**
* @private
*
* Application FlexEvent.CREATION_COMPLETE handler
*
* @param event
*/
private function _creationCompleteHandler(event:FlexEvent):void
{
_createTextInputs();
}
/**
* @private
*
* TextInput instances TextOperationEvent.CHANGE handler
*
* @param event
*/
private function _textInput_changeHandler(event:TextOperationEvent):void
{
_handleChangesInTextInput(TextInput(event.target));
}
/**
* @private
*
* TextInput instances FocusEvent.FOCUS_OUT handler
*
* @param event
*/
private function _textInput_focusOutHandler(event:FocusEvent):void
{
_handleChangesInTextInput(TextInput(event.currentTarget));
}
]]>
</fx:Script>
</s:Application>
如果您要监听TextOperationEvent.CHANGE
事件,那么任何按键(当 TextInput 具有输入焦点时)都会导致该_handleChangesInTextInput
方法被调用。
如果您要监听FocusEvent.FOCUS_OUT
事件,则_handleChangesInTextInput
在某些 TextInput 失去输入焦点后将调用方法。