<?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 が入力フォーカスを失った後にメソッドが呼び出されます。