2011-07-20 55 views
0

我是AS3和Flex的新手。我想在点击或按钮上添加一个文本区域,例如可以说一个人有多个地址,并且他们想要添加更多的地址。当用户点击“添加地址”的新文本区应appear.I到处找解决方案,但没有运气在AS3中动态创建文本区域oniflick

听到的是我已经尝试的代码(这可能是非常错误的):

import mx.controls.Alert; 
import mx.events.CloseEvent; 
private function createTextField(evt:Event):void{ 
    var theTextField:TextField = new TextField(); 
    theTextField.type = TextFieldType.INPUT; 
    theTextField.border = true; 
    theTextField.x = 10; 
    theTextField.y = 10; 
    theTextField.multiline = true; 
    theTextField.wordWrap = true; 
    addChild(theTextField); 
} 

<mx:FormItem> 
<mx:Button label="Add Text Area" click="createTextField(event);"/> 
</mx:FormItem> 

在此先感谢任何可以帮助的人。

回答

0

您正在混合基于Flash和Flex的组件 - 因为您使用的是mxml,我假设您使用的是flex。

mx命名空间是“旧”(pre flex4组件) - 您可能需要使用spark-components,因此也需要使用s-namespace。

<s:Button /> 

,如果你想将组件添加到你的MXML动态你不使用的addChild(如/闪光),但的addElement()。最后,你没有创建一个基于闪存的文本字段,但一个柔性基地的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> 
0

它很简单,看看下面的例子中,我提出:

<?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> 

只需添加一个事件使用其click属性收听Button元素。然后在事件处理程序中创建一个TextArea对象,并使用其addElement()方法将其添加到应用程序中。

这里是正在运行Flex应用的图像:

enter image description here