点击外部,从输入中删除焦点

时间:2015-10-06 18:00:01

标签: actionscript-3 flex flex4

我正在使用带有火花组件的Flex 4.6。我有DropDownList的表单,我想实现下一个行为:

用户点击DropDownList中的文字输入 - > DropDownList获得焦点

用户点击外部(例如,在背景矩形上) - > DropDownList失去焦点

突然间,第二部分无效。当用户在文本输入外部单击时,DropDownList仍然处于焦点。如何实现所需的行为?

示例:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark">
    <fx:Script><![CDATA[
        ]]></fx:Script>
    <s:TextInput />
</s:Application>

如果单击TextInput,它将获得焦点。并且您无法通过单击TextInput外部来删除此焦点。

2 个答案:

答案 0 :(得分:2)

我在官方论坛找到了解决方案。有一个post有同样的问题。 最后的工作解决方案:

textInput.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, textInputMouseFocusChange, false, 0, true);
// basically the above Event will dispatch when ever the user clicked outside of textinput when the current focus in in textInput.

private function textInputMouseFocusChange(event:FocusEvent):void {
// basically u dont need this first line if u are not calling any function on textInput focusout.

    textInput.dispatchEvent(new FocusEvent(FocusEvent.FOCUS_OUT));

// the remaing peice of code will remove the current focus from textInput to the area where the user had clicked.
// this piece of code i have taken from one of the comments in the blog : http://blog.flexexamples.com/2008/09/23/setting-focus-in-flex-using-the-focus-manager/

    var focusPoint:Point = new Point();
    focusPoint.x = stage.mouseX;
    focusPoint.y = stage.mouseY;
    var i:int = (stage.getObjectsUnderPoint(focusPoint).length);
    stage.focus=InteractiveObject(stage.getObjectsUnderPoint(focusPoint)[i-1].parent);
}

答案 1 :(得分:1)

首先,我认为您所说的是ComboBox spark组件,而不是DropDownList,因为您已经提到TextInput组件LabelDropDownList

的情况下控制

然后,要从ComboBox组件移除焦点,您可以在触发组件的更改事件时将stage.focus设置为null

<s:ComboBox change="on_Change(event)" />

protected function on_Change(event:Event):void
{
    stage.focus = null;
}

希望可以提供帮助。