actionscript-3 - 如何在 for 循环中为 Actionscript 中的标签/按钮创建唯一的事件监听器?

标签 actionscript-3 apache-flex flex4 flex4.5

目前,我在 Actionscript(适用于 Flex)中使用此命令在 BorderContainer 中创建 26 个标签(ID 字母):

var lettersArray:Array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

for(var indexLetter:String in lettersArray) {
    var labelLetter:Label = new Label();
    labelLetter.text = lettersArray[indexLetter];
    labelLetter.addEventListener(MouseEvent.CLICK, function():void { getUsers(lettersArray[indexLetter]); });

    letters.addElement(labelLetter);
}

但是当我点击一个字母时,只有字母 Z 被发送到函数 getUsers。因此,当我从 for 循环创建标签时,只有事件监听器适用于最后创建的标签。我该如何解决这个问题?

最佳答案

我假设 getUsers(lettersArray[indexLetter]) 是一个错误,因为您无法访问这样的数组元素

var lettersArray:Array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

for each(var letter:String in lettersArray) {
    var labelLetter:Label = new Label();
    labelLetter.text = letter;
    labelLetter.addEventListener(MouseEvent.CLICK, function():void { getUsers(e.currentTarget.text); });

    letters.addElement(labelLetter);
}

关于actionscript-3 - 如何在 for 循环中为 Actionscript 中的标签/按钮创建唯一的事件监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9329494/

相关文章:

flash - 是否可以以8KHz采样数据速率而不是44.1KHz获得声音原始数据?

actionscript-3 - Adobe AIR 能否检索运行它的设备的硬件规范?

linux - Flex : Fails to compile . 作为文件

apache-flex - Flex 轻松将按钮模式设置为所有按钮

apache-flex - Flex 中非常自定义/水平的 TreeView

actionscript-3 - 如何在组容器上执行鼠标绘制的线的复制粘贴操作?

apache-flex - 如何过滤闪光灯(10.1)中来自麦克风的声音?

css - 如何在 Actionscript 3 中粘贴具有相同属性的文本?

forms - FormItems 未与 DataGroup 中的 ItemRenderer 对齐

apache-flex - 错误 : Could not resolve to a component implementation