如何将 Focus_out 事件应用于不仅仅是选定的组件
我正在使用基于此的代码
它使用 focus_out 事件来检测是否有需要提交的更改。但是我注意到,只有在远离文本字段但在组件内部单击时才会调用 FOCUS_OUT 事件。有什么方法可以从组件内部监听组件外部的点击吗?
addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
protected function onFocusOut(event:FocusEvent):void
{
_updatedText = text;
if(_updatedText != _originalText){
dispatchEvent(new Event(Event.CHANGE));
}
setEditable(false);
}
I'm using code based on this post.
It uses a focus_out event to detect if there is a change that needs to be committed. However I notice that a FOCUS_OUT event is only called if you click away from the textfield but inside the component. Is there any way I can listen for clicks outside the component from within the component?
addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
protected function onFocusOut(event:FocusEvent):void
{
_updatedText = text;
if(_updatedText != _originalText){
dispatchEvent(new Event(Event.CHANGE));
}
setEditable(false);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在组件本身中,您可以执行以下操作:
只需确保在从舞台中删除组件之前清理并删除事件侦听器(假设它是动态添加的)。这将阻止您堆积一堆事件侦听器。
或者,如果您只想了解何时有人在特定组件之外单击,您可以执行以下操作:
同样,如果动态添加/删除该组件,请确保清除所有事件侦听器。
希望这有帮助!
编辑:
如果您想清理事件监听器,请执行以下操作(在组件中触发删除事件时调用):
显然替换您最终使用的事件监听器(焦点或鼠标)。
In the component itself, you can do this:
Just be sure you clean up and remove the event listener before your component is removed from the stage (assuming it is added dynamically). That will prevent you from stacking up a bunch of event listeners.
Alternatively, if you just want to find out whenever someone clicks outside of a specific component, you can do something like this:
Again, make sure you cleanup any event listeners if this component is added/removed dynamically.
Hope this helps!
EDIT:
If you want to cleanup the eventListeners, do something like this (called when the remove event is triggered in your component):
Obviously substitute the event listeners that you ended up using (Focus or Mouse).
在 LabelEditor 类中,在焦点移出时调度 Event.CHANGE 事件,您可以只监听该事件
In LabelEditor class dispatches a Event.CHANGE event on focus out you can just listen for that event