如何将 Focus_out 事件应用于不仅仅是选定的组件

发布于 2024-11-01 23:25:16 字数 440 浏览 1 评论 0原文

我正在使用基于此的代码

它使用 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 技术交流群。

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

发布评论

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

评论(2

ぶ宁プ宁ぶ 2024-11-08 23:25:16

在组件本身中,您可以执行以下操作:

systemManager.addEventListener( FocusEvent.KEY_FOCUS_CHANGE, focusChangeHandler );
systemManager.addEventListener( FocusEvent.MOUSE_FOCUS_CHANGE, focusChangeHandler );

只需确保在从舞台中删除组件之前清理并删除事件侦听器(假设它是动态添加的)。这将阻止您堆积一堆事件侦听器。

或者,如果您只想了解何时有人在特定组件之外单击,您可以执行以下操作:

systemManager.addEventListener( MouseEvent.MOUSE_DOWN, system_mouseDownHandler );

private function system_mouseDownHandler( event:MouseEvent ):void {
  if( !event.target != this && !this.contains(event.target as DisplayObject) ){
    // Do Something Here
  }
}

同样,如果动态添加/删除该组件,请确保清除所有事件侦听器。

希望这有帮助!

编辑:

如果您想清理事件监听器,请执行以下操作(在组件中触发删除事件时调用):

<mx:Component remove="myRemoveHandler();" />

private function myRemoveHandler():void {
  if( systemManager.hasEventListener( MouseEvent.MOUSE_DOWN ) systemManager.removeEventListener( MouseEvent.MOUSE_DOWN, system_mouseDownHandler );
}

显然替换您最终使用的事件监听器(焦点或鼠标)。

In the component itself, you can do this:

systemManager.addEventListener( FocusEvent.KEY_FOCUS_CHANGE, focusChangeHandler );
systemManager.addEventListener( FocusEvent.MOUSE_FOCUS_CHANGE, focusChangeHandler );

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:

systemManager.addEventListener( MouseEvent.MOUSE_DOWN, system_mouseDownHandler );

private function system_mouseDownHandler( event:MouseEvent ):void {
  if( !event.target != this && !this.contains(event.target as DisplayObject) ){
    // Do Something Here
  }
}

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):

<mx:Component remove="myRemoveHandler();" />

private function myRemoveHandler():void {
  if( systemManager.hasEventListener( MouseEvent.MOUSE_DOWN ) systemManager.removeEventListener( MouseEvent.MOUSE_DOWN, system_mouseDownHandler );
}

Obviously substitute the event listeners that you ended up using (Focus or Mouse).

晨与橙与城 2024-11-08 23:25:16

在 LabelEditor 类中,在焦点移出时调度 Event.CHANGE 事件,您可以只监听该事件

In LabelEditor class dispatches a Event.CHANGE event on focus out you can just listen for that event

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