【问题标题】:Flex: Lose Component FocusFlex:失去组件焦点
【发布时间】:2011-04-02 22:40:45
【问题描述】:

我有一个(希望是)简短的问题。我有一些步进箱。虽然这确实适用于任何交互式组件。当我单击其他任何位置(包括舞台)时,我希望所选框失去焦点。是否有捷径可寻?我似乎找不到有效的方法让它失去焦点。

【问题讨论】:

  • 点击别处不会自动失去焦点吗?
  • 我无法做到。如果我点击其他任何地方,一旦我选择了一个组件,我就只能选择另一个组件。我无法通过单击舞台取消选择所有组件。

标签: apache-flex focus components


【解决方案1】:

如果其他人在这里找到解决此问题的方法,这里是答案:

private function onGlobalMouseUp(event : MouseEvent) : void {
        var fm:FocusManager = new FocusManager(stage);

        //Since Flash Player can set focus on subcomponents as well as on components themselves, 
        //findFocusManagerComponent is used to find the component that either has focus or contains the subcomponent 
        //that has focus. If, for example, a TextField contained within a TextArea component has focus, findFocusManagerComponent 
        //will return the TextArea component, and not the TextField.  This being the case, we can definitely determine 
        //whether the target object of the MouseUp event is a component (or is part of a component).  If the target
        //object is NOT a component (nor contained within one), then we clear all component focus.

        if(fm.findFocusManagerComponent(event.target as InteractiveObject) is UIComponent){
            //target of MouseUp is either a UIComponent, or is contained within a UIComponent, so do nothing.
        }else{
             //target of MouseUp is neither a UIComponent, nor is it contained within a UIComponent, so set component focus to null.
            fm.setFocus(null);
        }
    }

【讨论】:

    【解决方案2】:

    所以这是我想出的非常有效的解决方案。我有一个名为add() 的函数,它已分配给applicationComplete。在该功能中,我包括:

    this.skin.addEventListener( MouseEvent.MOUSE_UP, loseFocus );
    

    调用者:

    private function loseFocus( e : MouseEvent ) : void
    {
        if ( e.eventPhase == EventPhase.AT_TARGET )
        {
            this.focusManager.deactivate();
        }
    }
    

    足够简单,并且可以满足我的要求。 “阶段”过滤器是必要的,以防止其他组件注册点击。

    作为重要说明:this.skin 需要成为事件目标。在 Flex 应用程序中,舞台永远不会暴露给鼠标。

    Example Application Code

    如果有人有更好的解决方案,请提出一个!

    【讨论】:

      【解决方案3】:

      也许你应该看看FocusManager.hideFocus()

      也许将它与 UIComponent 的 focusOut 事件联系起来。

      Flex API

      【讨论】:

      • 也许我不明白,但我不只是对隐藏指标感兴趣。如果我隐藏它并且它实际上仍然被选中,那么其他交互(如点击向上和向下箭头)将导致步进器递增或递减。我想取消选择任何选定的组件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      相关资源
      最近更新 更多