开发者

Flex: assigning events to dynamically created buttons

开发者 https://www.devze.com 2023-03-21 09:48 出处:网络
My app has buttons that users press to insert predefined strings into a textarea. I\'m now making it load in the button values dynamically so users can define their own buttons.

My app has buttons that users press to insert predefined strings into a textarea. I'm now making it load in the button values dynamically so users can define their own buttons.

I'm using a buttons.txt which contains a different label on each line (button1, button2, button3 etc). I loop through the text file and add the buttons to a group. This all works, but now the hard part. How can I assign an eventlistener to these buttons so that they output text to the screen?

protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
        {
            var path:File = File.documentsDirectory.resolvePath("buttons.txt");
            myTextLoader.load(new URLRequest("file://" +path.nativePath));
            myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
            trace(path.nativePath); // Traces correct file path

            mainTextField.addEventListener(SoftKeyboardEvent.SOFT_KEYBOARD_ACTIVATE, resizeTextField);
            if(data!=null){
                mainTextField.text = data.toString();
            }else{
                mainTextField.text = tags;
            }
        }

        protected function onLoaded(e:Event):void {
            var myArrayOfLines:Array = e.target.data.split(/\n/);
            var tempBtn:Button;
            trace(myArrayOfLines[0]);
            for(var i:Number = 0;i < myArrayOfLines.length;i++){
                tempBtn = new Button();
                tempBtn.label = myArrayOfLines[i];
                btnArray.push(tempBtn);
                group.addElement(btnArray[i]);
            }
        }

EDIT

    protected function onLoaded(e:Event):void {
                var myArrayOfLines:Array = e.target.data.split(/\n/);
                var tempBtn:Button;

                for(var i:Number = 0;i < myArrayOfLines.length;i++){
                    var j:Number = i+1;
                    tempBtn = new Button();
                    tempBtn.id = "btn" + i;
                    tempBtn.addEventListener(MouseEvent.CLICK, function(evt:MouseEvent):void{
                        var index:uint = parseInt(evt.currentTarget.id.replace("btn", ""));
                                    //TextArea code will go here

trace(text); //开发者_如何学运维 Traces null
                    });
                    tempBtn.label = myArrayOfLines[i];
                    btnArray.push(tempBtn);
                    group.addElement(btnArray[i]);
                }
            }


buttons.txt
     button1_label
    "Hello"
    button2_label
    "Goodbye"
    button3_label
    "Come again"


it's not clear if the text that you want to add is the same label of the button or a different text. Anyway when you create the button you can add an event lister. suppose that txtArr is a simple array containing the string that you want to add

for(var i:Number = 0;i < myArrayOfLines.length;i++){
    tempBtn = new Button();
    tempBtn.id = "btn" + i;
    tempBtn.addEventListener(MouseEvent.CLICK, function(evt:MouseEvent):void{
        var index = parseInt(evt.currentTarget.id.replace("btn", ""));
        var text:String = textArr[i] as String;
    });
    tempBtn.label = myArrayOfLines[i];
    btnArray.push(tempBtn);
    group.addElement(btnArray[i]);
}

To put it simple just use the id field to store the current id of the button then when the event is fired take the index number from the id and read the text from the array. Then you just have to add the text string to your textarea

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消