2010-08-24 167 views
2

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

+0

它不会自动失去焦点,当您单击其他地方? – JeffryHouser 2010-08-24 01:38:04

+0

不能我管理。如果我点击其他任何地方,一旦选择了一个组件,我只能选择另一个组件。我不能通过点击舞台来取消选择所有组件。 – grey 2010-08-24 01:41:32

回答

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

也许你应该检查出FocusManager.hideFocus()

也许将它绑定到您的UIComponent的focusOut事件。

Flex API

+0

也许我不明白,但我不只是对隐藏指标感兴趣。 如果我将它隐藏起来并且它仍然被选中,那么其他相互作用如敲击向上和向下箭头将导致步进器递增或递减。我想取消选择任何选定的组件。 – grey 2010-08-24 01:43:22

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

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

+0

你钉了:)谢谢! – mik 2012-02-09 22:00:28