Flex:将事件分配给动态创建的按钮

发布于 2024-11-25 04:58:39 字数 3095 浏览 1 评论 0原文

感谢 wezzy 和其他人在过去几天里帮助我。我一直在听取他们的建议,并试图自己解决剩下的问题,但我再次陷入困境。

我有一个 txt 文件,结构如下:

button1_label
button2_label
button3_label

我的程序在运行时创建这 3 个按钮并将它们放在一个组中。

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"

所以我想做的是让button1打印“Hello”。 .txt 文件的所有行都被推送到 myArrayOfLines 中。预先感谢您的任何帮助。

编辑 完整解释

抱歉,我试图简化我的问题,我猜我让它变得更难理解。我制作了一个文本编辑器,运行客户端,没有服务器。我有一组按钮,可将预定义的短语插入 TextArea 每个按钮都有一个执行 myTextArea.insert("sometext"); 操作的侦听器(但每个按钮都有不同的文本。用户请求能够创建自己的按钮来插入自己的字符串。我想我会有几个 og 文本输入,用户可以在其中定义标签,以及将插入到单击按钮时的文本区域。我会将标签写入一行,然后将字符串写入下一行。现在我使用这种格式创建了一个 Buttons.txt 文件,以查看它是否有效

。 /强>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏九 2024-12-02 04:58:39

尝试一下,让我知道它是如何运作的...

编辑:(尝试下面的新代码)

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

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]);
            }
        }           
稀香 2024-12-02 04:58:39

第一:不要在动作脚本中使用匿名函数作为侦听器 - 如果你这样做,以后就无法删除它们。

而是使用这样的类方法:

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

刚刚看到,您正在使用 ID,然后只需将上面的代码更改为这样:

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

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文