将输入文本显示到另一个文本字段flash as3

时间:2011-10-10 09:11:28

标签: actionscript-3

请帮帮我......

我正在尝试在运行时将输入文本显示到另一个文本字段。我想表明 myOutputBox在影片剪辑中。代码如下:

动作脚本3

package

{

import flash.display.Sprite;

import flash.display.Stage;

import flash.text.*;

import flash.events.*;



public class CaptureUserInput extends Sprite

{

    private var myTextBox:TextField = new TextField();

    private var myOutputBox:TextField = new TextField();

    private var myText:String = "Type your text here.";



    public function CaptureUserInput()

    {

        captureText();

    }



    public function captureText():void

    {

        myTextBox.type = TextFieldType.INPUT;

        myTextBox.background = true;

        addChild(myTextBox);

        myTextBox.text = myText;

        myTextBox.addEventListener(TextEvent.TEXT_INPUT, textInputCapture);

    }



    public function textInputCapture(event:TextEvent):void

    {

        var str:String = myTextBox.text;

        createOutputBox(str);

    }



    public function createOutputBox(str:String):void

    {

        myOutputBox.background = true;

        myOutputBox.x = 200;

        addChild(myOutputBox);

        myOutputBox.text = str;

    }



}

}

1 个答案:

答案 0 :(得分:1)

修正了一些你的代码,并添加了一些东西,希望这可以帮助你:

  public class CaptureUserInput extends Sprite
  {

    private var initialText:String = "Type your text here.";

    public var myTextBox:TextField = new TextField();

    public var myOutputBox:TextField = new TextField();

    public function CaptureUserInput()
    {
        captureText(); 
    }

    public function captureText():void 
    { 
        createInputBox();

        createOutputBox(); 

        myTextBox.text = initialText;

        //reset input field so user can write
        myTextBox.addEventListener(FocusEvent.FOCUS_IN, focusInputIn);

        //capture text
        myTextBox.addEventListener(TextEvent.TEXT_INPUT, textInputCapture);

    }

    //this is almost your code, refactored in a function for clarity
    public function createInputBox():void
    {

        myTextBox.type = TextFieldType.INPUT; 
        myTextBox.background = true; 

        myTextBox.y = 100;
        addChild(myTextBox);
    }

    //just set the text of the output to the contents of the input  
    public function textInputCapture(event:TextEvent):void 
    { 
      myOutputBox.text = myTextBox.text;
    }

    public function createOutputBox():void 
    { 

       myOutputBox.y = 200;
       addChild(myOutputBox); 

    } 

    public function focusInputIn(event:Event):void
    {
      if(myTextBox.text == initialText)
        myTextBox.text ="";
    }

 }