2010-08-24 191 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