简体   繁体   中英

Flex: assigning events to dynamically created buttons

Thanks to wezzy and the others who helped me out the past couple of days. I've been taking their advice and trying to figure the rest out on my own but I'm stuck again.

I have a txt file structured like this:

button1_label
button2_label
button3_label

My program creates these 3 buttons at runtime and places them in a group.

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

Now what you can see from my code is that I'm attempting to make each button print a string to a textarea. I've structured my new buttons.txt like this:

button1_label
"Hello"
button2_label
"Goodbye"
button3_label
"Come again"

So what I want to do is have button1 print "Hello". All the lines of the.txt file are pushed into myArrayOfLines. Thanks in advance for any help.

EDIT Full explanation

Sorry I was trying to simplify my question, guess I made it harder to understand. I made a text editor than runs client side, no server. I have a set of buttons that insert predefined phrases into the TextArea Each button has a listener that does myTextArea.insert("sometext"); (but different text for each button. Users have requested the ability to create their own buttons to insert there own strings. I figured I would have a couple og textinputs where a user could define a label, and a String that would be inserted into the textarea on button click. I would write the label to one line, then the String to the next line. Right now I created a buttons.txt file with this format to see if it would work.

FINAL EDIT: WORKING CODE

public function setupBtns(e:Event):void{
            var file:File = File.documentsDirectory.resolvePath("buttons.txt");
            var stream:FileStreamWithLineReader = new FileStreamWithLineReader();
            stream.open(file, FileMode.READ);

            while(stream.bytesAvailable) {
                // this line contains the headers like button1_label etc.
                var label:String;
                // this line contains the string

                if(stream.bytesAvailable) {
                    // this line contains the actual label like "Hello";
                    label = stream.readUTFLine();
                    line = stream.readUTFLine();

                    // strip off the first and last character as they are double quotes
                    line = line.substring(1, line.length-1);

                    var tempBtn:Button = new Button();
                    tempBtn.addEventListener(MouseEvent.CLICK, btnListener);

                    tempBtn.label = label;
                    tempBtn.name = line;
                    btnArray.push(tempBtn);
                    for(var i:Number = 0;i<btnArray.length;i++){
                        group.addElement(btnArray[i]);
                    }
                }
            }
        }           

        protected function btnListener(e:MouseEvent):void{
            mainTextField.insertText(e.target.name);    
            trace(e.target.name);
        }

Try this out and let me know how it works out...

EDIT: (try new code below)

        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=i+1){
                var j:Number = i+1;
                tempBtn = new Button();
                tempBtn.id = "btn" + i;
                tempBtn.addEventListener(MouseEvent.CLICK, function(evt:MouseEvent):void{
                        myTextArea.text += '\n' + myArrayOfLines[j];
                    });
                tempBtn.label = myArrayOfLines[i];
                btnArray.push(tempBtn);
                group.addElement(btnArray[btnArray.length-1]);
            }
        }           

first: don't use anonymous functions as listeners in actionscript - if you yo, you can't remove them later on.

instead use a class method like this:

tempBtn.addEventListener(MouseEvent.CLICK, onMouseClick);

private function onMouseClick(evt:MouseEvent):void
{
  if (evt.currentTarget == button1_label)
  {
     // do sth for btn1
  }
  else if (evt.currentTarget == button2_label)
  {
     // do sth for btn2
  }
  // ...
}

edit

just saw, that you are using IDs, then just change the above code to sth like this:

if (evt.currentTarget.id == "btn1")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM