类中的 Flex 按钮事件监听器
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于 Button 的单击事件会冒泡,因此您只需侦听主应用程序文件上的单击事件并委托给类中的处理函数即可。
或者您可以通过单击按钮直接调用处理程序。
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.
看起来您有两个不同的选择或问题。 如果将最后一个参数更改为
false
,那么一切都会正常,因为您的事件侦听器将继续处理鼠标单击。 当然,这意味着如果您第二次单击“检查器”按钮,您将有两组侦听器响应鼠标单击按钮一、二和三。因此,您感兴趣的真正解决方案可能是保留上面引用的行相同,而是更改以下行:
如果您更改上面的行以将按钮侦听器存储为类的一部分,则它将可用于响应鼠标点击,而不是被垃圾收集:
当然,假设您已在 mx:script 块中定义了
_buttonListener
:按评论编辑:
在提供的代码中,
t
,ButtonListener
,超出了范围。 当它发生时,除非您使用强引用,否则它将被垃圾收集,而您在addEventListener
调用中不使用最后一个参数。因此,使按钮侦听器成为主类的成员:
Main.mxml 将显示为:
由于事件侦听器将不再超出范围,因此事件侦听器使用的弱引用将用作预期的,当 __buttonListener 超出范围时被垃圾收集。
It looks like you have two different options or problems. If you change the last parameter in:
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:
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:
That, of course, assumes that you have defined
_buttonListener
within an mx:script block:EDIT per comment:
In the code provided,
t
, theButtonListener
, 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 youraddEventListener
call.Thus, make the button listener a member of the main class:
Main.mxml would then read:
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.