Dynamically creating multiple textboxes that are populated with XML data

偶尔善良 提交于 2019-12-11 18:32:02

问题


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.

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

Thanks in advance!!


回答1:


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.)

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

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);
    }
}


来源:https://stackoverflow.com/questions/14761667/dynamically-creating-multiple-textboxes-that-are-populated-with-xml-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!