在TextArea外部单击时禁用TextArea ala Facebook

时间:2009-12-29 23:15:45

标签: flex events

我有一个textArea和一个Button - 当用户点击应用程序窗口中的任意位置时,我希望Button消失除了“发送”按钮

<mx:Button x="306" y="168" label="Button" id="btn" click="Alert.show('Button clicked')"/>
<mx:TextArea x="138" y="146" focusOut="btn.visible=false" focusIn="btn.visible=true"/>

当TextArea失去焦点时,我尝试调用btn.visible = false(使用focusOut事件) - 如果我单击应用程序中的任何位置它可以工作,但是当我单击按钮时它也有效 - 首先处理TextArea focusOut事件并且稍后点击按钮 - 有人可以帮忙吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

我之前曾说使用过电话,但在测试时它不起作用,抱歉浪费任何人的时间。相反,你需要使用像这样的焦点管理器:我测试了它,看起来很稳固。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
    import mx.controls.Alert;
    import mx.managers.FocusManager; //pull in the manager

    private function onFocusOut(event:FocusEvent):void{
            if(getFocus() != null){ //in case focus goes outside the flash player
                if(getFocus().name == "btn"){ //the focus went to the item with the ID "btn"
                    return; //do nothing, let the click handler work
                }else{ //any other item gets focus
                    btn.visible=false;  //disappear 
                }
            }
        }

    private function clickHandler():void{ // made it it's own function so do more than just alert
        Alert.show('Button clicked');
        btn.visible=false;
    }
]]>
</mx:Script>

    <mx:Button x="306" y="168" label="Button" id="btn" click="clickHandler();"/>
    <mx:TextArea x="138" y="146" focusOut="onFocusOut(event)" focusIn="btn.visible=true"/>

</mx:Application>

答案 1 :(得分:0)

你试图覆盖默认的focusOut处理程序吗?这不起作用......

private function setBtnNotVisible():void
{
    btn.visible=false;
}
override protected function focusOutHandler(event:FocusEvent):void
{
    callLater(setBtnNotVisible);
}

也没有写我自己的事件处理程序......

private function setBtnNotVisible():void
{
    btn.visible=false;
}
private function focusOutHandler2(event:FocusEvent):void
{
    callLater(setBtnNotVisible);
}

---------------------完整代码------------------

#####尝试单击INSIDE textArea然后在按钮上,我无法捕获警报
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
    import mx.controls.Alert;

    private function onFocusOut(event:FocusEvent):void{
            callLater(
                function ():void{
                    btn.visible=false;
               }
            )
        }
]]>
</mx:Script>

    <mx:Button x="306" y="168" label="Button" id="btn" click="Alert.show('Button clicked')"/>
    <mx:TextArea x="138" y="146" focusOut="onFocusOut(event)" focusIn="btn.visible=true"/>

</mx:Application>