动态创建被填充了XML数据的多个文本框多个、文本框、动态、数据

2023-09-08 15:07:23 作者:〗籿銕殘惢〖

我应该进一步说明这一点。 在我的AS3,我目前正在创建动态文本从XML表到一个文本框。 我的codeS看起来像这样来帮助描述这种进一步的:

I should further describe this. In my AS3, I am currently creating dynamic text from an XML sheet into a textfield. my codes looks like this to help describe this further:

    function XMLLoader(e:Event):void
    {
        xmlData = new XML(e.target.data);
        ParseList(xmlData);

    }

    function ParseList(nameData:XML):void
    {
     var nameList:XMLList = nameData.Searchtext.Name;
     for each (var nameElement:XML in nameList){
      directory_text.appendText(nameElement.text() + "\n");
     trace(nameElement.text());
     }
    }

我想使用的是同一种格式做什么,创造输出的每个项目的文本框(在本例中的名称),这样我就可以使每个单独的项目可点击并添加功能。我希望有这样做为每一个,但那里只是心不是一个清晰的方式来做到这一点。

What I want to do is using the same sort of format, create a text box for EACH item outputted (in this case names) so that I can make each seperate item clickable and add a function. I was hoping to do this with "for each" but there just isnt a clear way to do it.

任何想法?在我所有的AS3电话列在了,这可以达到50多个项目,所以我要动态地添加它。

Any ideas? my AS3 calls in all listed under , and this can reach up to 50+ items, so I want to add it dynamically.

在此先感谢!

推荐答案

这是做你问一个简单的例子。这增加了一个文本字段为每一位的名称的节点在一个容器雪碧对象,然后添加一个事件监听器容器赶在容器上出现的任何点击(和它的孩子们。)

This is a simple example of doing what you asked. This adds a TextField for every Name node in a container Sprite object and then adds an event listener to the container to catch any clicks that occur on the container (and it's children.)

在事件监听器方法,计算出该文本字段 s表示是点击,追溯其内容的实例。

In the event listener method, it figures out which instance of the TextFields that was clicked and traces its contents.

您可能不能立即使用code,但它显示了如何像可以做到这一点的原则。

You likely cannot use this code immediately, but it shows the principle of how something like this can be done.

function parseList(nameData:XML):void {
    var nameList:XMLList = nameData.Searchtext.Name;

    var textContainer:Sprite = new Sprite();
    this.addChild(textContainer);

    for each (var node:XML in nameList) {
        var currTextField:TextField = new TextField();
        currTextField.text = node.text();
        currTextField.y = textContainer.height; //Place the textfield below any previous textfields
        currTextField.height = 30;
        currTextField.selectable = false;
        textContainer.addChild(currTextField);
    }

    textContainer.addEventListener(MouseEvent.CLICK, onTextClick);
}

function onTextClick(e:MouseEvent):void {
    if (e.target is TextField) {
        var currTextField:TextField = e.target as TextField;
        trace("This name was clicked:", currTextField.text);
    }
}
 
精彩推荐
图片推荐