Actionscript 3 在AS3中单击时动态创建文本区域

Actionscript 3 在AS3中单击时动态创建文本区域,actionscript-3,Actionscript 3,我是AS3和Flex的新手。我想在点击或按钮上添加一个文本区域,例如,如果一个人有多个地址,他们想添加更多地址。当用户单击“添加地址”时,会出现一个新的文本区域。我已经到处寻找解决方案,但运气不佳 Hear是我尝试过的代码(这可能是非常错误的): 导入mx.controls.Alert; 导入mx.events.CloseEvent; 私有函数createTextField(evt:Event):void{ var theTextField:TextField=newtextfield(); t

我是AS3和Flex的新手。我想在点击或按钮上添加一个文本区域,例如,如果一个人有多个地址,他们想添加更多地址。当用户单击“添加地址”时,会出现一个新的文本区域。我已经到处寻找解决方案,但运气不佳

Hear是我尝试过的代码(这可能是非常错误的):

导入mx.controls.Alert;
导入mx.events.CloseEvent;
私有函数createTextField(evt:Event):void{
var theTextField:TextField=newtextfield();
textfield.type=TextFieldType.INPUT;
theTextField.border=true;
文本字段x=10;
文本字段y=10;
theTextField.multiline=true;
theTextField.wordWrap=true;
addChild(文本字段);
}

提前感谢任何能提供帮助的人

您混合了基于flash和基于flex的组件—因为您使用的是mxml,所以我假设您使用的是flex

mx名称空间是“旧的”(flex4之前的组件)-您可能希望使用spark组件,因此使用s名称空间

<s:Button />

如果您想动态地将组件添加到mxml中,则不使用addChild(as/flash),而是使用addElement()。最后,您不会创建基于flash的TextField,而是创建基于flex的TextInput:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   creationComplete="onCreationComplete(event);"
                   >
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

    <fx:Script>
      <![CDATA[

        import spark.components.TextInput;

        private function onCreationComplete(evt:Event):void
        {
            trace("onCreationComplete()");
            var txt:TextInput = new TextInput();
            grp.addElement(txt);
        }

    ]]>
  </fx:Script>

  <s:VGroup id="grp">
  </s:VGroup>

</s:WindowedApplication>

这真的很简单,看看我做的以下示例:

<?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"
               width="250" height="250">

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[

        import flash.events.Event;
        import spark.components.TextArea;

        protected function onButtonClick(e:Event):void
        {
            var textArea:TextArea = new TextArea();
            textArea.id = "textArea";
            addElement(textArea);

        }// end function

        ]]>
    </fx:Script>

    <s:layout>
        <s:VerticalLayout></s:VerticalLayout>
    </s:layout>

    <s:Button id="button" click="onButtonClick(event)">Add Text Area</s:Button>

</s:Application>

添加文本区域
只需使用
单击
属性将事件侦听器添加到
按钮
元素即可。然后在事件处理程序中创建一个
TextArea
对象,并使用其
addElement()
方法将其添加到应用程序中

以下是正在运行的flex应用程序的图像:

<?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"
               width="250" height="250">

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[

        import flash.events.Event;
        import spark.components.TextArea;

        protected function onButtonClick(e:Event):void
        {
            var textArea:TextArea = new TextArea();
            textArea.id = "textArea";
            addElement(textArea);

        }// end function

        ]]>
    </fx:Script>

    <s:layout>
        <s:VerticalLayout></s:VerticalLayout>
    </s:layout>

    <s:Button id="button" click="onButtonClick(event)">Add Text Area</s:Button>

</s:Application>