Flex:失去组件焦点

时间:2010-08-24 01:08:05

标签: flex focus components

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

3 个答案:

答案 0 :(得分:3)

如果有其他人在这里找到解决这个问题的方法,可以回答:

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);
        }
    }

答案 1 :(得分: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

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

答案 2 :(得分:0)

也许您应该查看FocusManager.hideFocus()

可能将它绑定到UIComponent的focusOut事件中。

Flex API