Flex: потерять фокус компонента - PullRequest
2 голосов
/ 24 августа 2010

У меня (надеюсь) быстрый вопрос. У меня есть несколько степперов. Хотя на самом деле это может относиться к любому интерактивному компоненту. Я хочу, чтобы выбранное поле теряло фокус, когда я щелкаю где-либо еще (включая сцену). Есть простой способ сделать это? Кажется, я не могу найти эффективный способ заставить его потерять фокус.

Ответы [ 3 ]

3 голосов
/ 08 февраля 2011

В случае, если кто-то еще найдет способ найти решение этой проблемы, вот ответ:

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 голосов
/ 24 августа 2010

Итак, вот решение, которое я придумала, которое работает очень хорошо. У меня есть функция с именем 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.

Пример кода приложения

Если у кого-то есть лучшее решение, предложите его!

0 голосов
/ 24 августа 2010

Возможно, вам стоит проверить FocusManager.hideFocus().

Может быть, привязать его к событию focusOut вашего UIComponent.

Flex API

...