类中的 Flex 按钮事件监听器

发布于 2024-07-30 18:50:03 字数 1561 浏览 4 评论 0原文

我在 Main.mxml 中有很多按钮。 我正在尝试将按钮功能移至类中,并让类内的事件侦听器响应 Click 并调用其他函数。 我写过:

Main.mxml

<mx:Button x="23.5" y="10" label="checker" click="{goListen()}" />
<mx:Button id="btnT1" x="252.5" y="10" label="t1" />
<mx:Button id="btnT2" x="309" y="10" label="t2"/>
<mx:Button id="btnT3" x="366" y="10" label="t3"/>

Main.as

private function goListen():void
    {
        var t:ButtonListener = new ButtonListener(btnT1, btnT2, btnT3);
    }

ButtonListener.mxml

package com.util

{ 导入 flash.events.EventDispatcher; 导入 flash.events.MouseEvent;

import mx.controls.Alert;
import mx.controls.Button;

public final class ButtonListener extends EventDispatcher
{
    private var __btnArray:Array;

    public function ButtonListener(...btnList)
    {
        __btnArray = new Array();

        for each (var item:Button in btnList)
        {
            __btnArray.push(item);
        }

        buildListeners();
    }

    private function buildListeners():void
    {
        for each (var item:Button in __btnArray)
        {
            item.addEventListener(MouseEvent.CLICK, traceMe, false, 0, true);
        }
    }

    private function traceMe(event:MouseEvent):void
    {
        trace(event.target.label + " was clicked");
    }
}

因此,

当我调试时,我看到数组填满了按钮,但traceMe() 函数不起作用。 不知道如何让它发挥作用。 或者我只需要在主类中拥有 30 个事件侦听器和相应的函数。

I have many buttons in Main.mxml. I'm trying to move the button functionality into a Class and have Event Listeners inside the class respond to Click and call other functions. I have written:

Main.mxml

<mx:Button x="23.5" y="10" label="checker" click="{goListen()}" />
<mx:Button id="btnT1" x="252.5" y="10" label="t1" />
<mx:Button id="btnT2" x="309" y="10" label="t2"/>
<mx:Button id="btnT3" x="366" y="10" label="t3"/>

Main.as

private function goListen():void
    {
        var t:ButtonListener = new ButtonListener(btnT1, btnT2, btnT3);
    }

ButtonListener.mxml

package com.util

{
import flash.events.EventDispatcher;
import flash.events.MouseEvent;

import mx.controls.Alert;
import mx.controls.Button;

public final class ButtonListener extends EventDispatcher
{
    private var __btnArray:Array;

    public function ButtonListener(...btnList)
    {
        __btnArray = new Array();

        for each (var item:Button in btnList)
        {
            __btnArray.push(item);
        }

        buildListeners();
    }

    private function buildListeners():void
    {
        for each (var item:Button in __btnArray)
        {
            item.addEventListener(MouseEvent.CLICK, traceMe, false, 0, true);
        }
    }

    private function traceMe(event:MouseEvent):void
    {
        trace(event.target.label + " was clicked");
    }
}

}

so when I debug, I see the array filling up with the buttons, but the traceMe() function won't work. Not sure how I can get this to work. Or do I just have to have 30 event listeners and corresponding functions in the main class.

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

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

发布评论

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

评论(2

旧人哭 2024-08-06 18:50:09

由于 Button 的单击事件会冒泡,因此您只需侦听主应用程序文件上的单击事件并委托给类中的处理函数即可。

或者您可以通过单击按钮直接调用处理程序。

private var controller:ButtonListener = new ButtonListener();

<mx:Button id="btnT1" x="252.5" y="10" label="t1" click="controller.handleClick(event)"/>

Since the click event of Button bubbles, you can just listen for a click event on the main application file and delegate to a handler function in a class.

Or you can call the handler directly on the click of your button.

private var controller:ButtonListener = new ButtonListener();

<mx:Button id="btnT1" x="252.5" y="10" label="t1" click="controller.handleClick(event)"/>
故笙诉离歌 2024-08-06 18:50:08

看起来您有两个不同的选择或问题。 如果将最后一个参数更改为

item.addEventListener(MouseEvent.CLICK, traceMe, false, 0, true);

false,那么一切都会正常,因为您的事件侦听器将继续处理鼠标单击。 当然,这意味着如果您第二次单击“检查器”按钮,您将有两组侦听器响应鼠标单击按钮一、二和三。

因此,您感兴趣的真正解决方案可能是保留上面引用的行相同,而是更改以下行:

var t:ButtonListener = new ButtonListener(btnT1, btnT2, btnT3);

如果您更改上面的行以将按钮侦听器存储为类的一部分,则它将可用于响应鼠标点击,而不是被垃圾收集:

_buttonListener = new ButtonListener(btnT1, btnT2, btnT3);

当然,假设您已在 mx:script 块中定义了 _buttonListener

<mx:Script><![CDATA[
    var _buttonListener:ButtonListener;
 ]]></mx:Script>

按评论编辑:

在提供的代码中,tButtonListener,超出了范围。 当它发生时,除非您使用强引用,否则它将被垃圾收集,而您在 addEventListener 调用中不使用最后一个参数。

因此,使按钮侦听器成为主类的成员:

Main.mxml 将显示为:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Button x="23.5" y="10" label="checker" click="{goListen()}" />
    <mx:Button id="btnT1" x="252.5" y="10" label="t1" />
    <mx:Button id="btnT2" x="309" y="10" label="t2"/>
    <mx:Button id="btnT3" x="366" y="10" label="t3"/>
    <mx:Script>
        <![CDATA[
            private var _buttonListener:ButtonListener;

            private function goListen():void
            {                
                _buttonListener = new ButtonListener(btnT1, btnT2, btnT3); 
            }
        ]]>
    </mx:Script>
</mx:Application>

由于事件侦听器将不再超出范围,因此事件侦听器使用的弱引用将用作预期的,当 __buttonListener 超出范围时被垃圾收集。

It looks like you have two different options or problems. If you change the last parameter in:

item.addEventListener(MouseEvent.CLICK, traceMe, false, 0, true);

to false, then everything should work because your event listener will stick around to handle the mouse clicks. Of courses, this means that if you click on your "checker" button a second time, you'll then have two sets of listeners responding to mouse clicks of buttons one, two, and three.

So, it's likely that the real solution you're interested in is leaving the line quoted above the same and instead changing the following line:

var t:ButtonListener = new ButtonListener(btnT1, btnT2, btnT3);

If you change the above line to store your button listener as a part of your class it will be available to respond to the mouse clicks, rather than having been garbage collected:

_buttonListener = new ButtonListener(btnT1, btnT2, btnT3);

That, of course, assumes that you have defined _buttonListener within an mx:script block:

<mx:Script><![CDATA[
    var _buttonListener:ButtonListener;
 ]]></mx:Script>

EDIT per comment:

In the code provided, t, the ButtonListener, goes out of scope. When it does, it is garbage collected unless you use strong references, which you do not per the last parameter in your addEventListener call.

Thus, make the button listener a member of the main class:

Main.mxml would then read:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Button x="23.5" y="10" label="checker" click="{goListen()}" />
    <mx:Button id="btnT1" x="252.5" y="10" label="t1" />
    <mx:Button id="btnT2" x="309" y="10" label="t2"/>
    <mx:Button id="btnT3" x="366" y="10" label="t3"/>
    <mx:Script>
        <![CDATA[
            private var _buttonListener:ButtonListener;

            private function goListen():void
            {                
                _buttonListener = new ButtonListener(btnT1, btnT2, btnT3); 
            }
        ]]>
    </mx:Script>
</mx:Application>

Since the event listener will no longer go out of scope, the weak references used by the event listeners will work as expected, being garbage collected when __buttonListener goes out of scope.

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