0

actionscriptを使用してtextinputを動的に作成しましたが、フロントエンドから入力値を入力してdb(ファイル)に保存すると、dbに最後に入力された値のみが表示されます。助けてください

 for(var i:int=0;i<5;i++){
        var txt1:TextInput=new TextInput();
    txt1.width=88;
    createtxtinput(txt1);
    txt1.addEventListener(Event.CHANGE,test123(i,txt1))

var txt2:TextInput=new TextInput();
    txt2.width=88;
    createtxtinput(txt2);
    txt2.addEventListener(Event.CHANGE,test123(i,txt2))
  }

public function test123(x:int,txtmaterial:TextInput){
    return function(event){
    //  
        setValuestable2multi(x,txtmaterial.text);
//  Alert.show(x.toString());
    }

}
4

1 に答える 1

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

于 2012-05-24T12:33:19.380 に答える