使用事件侦听器的默认按钮切换器

时间:2011-07-29 06:28:47

标签: flex event-listener

我尝试将最近的按钮设为单击的文本输入默认值。为此,我写了下面的代码。

首先,为什么我的buttonSwitcher函数跟在MouseEvent.CLICK后面? 其次,有更好的方法吗?

提前致谢

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="addListeners()">
    <mx:Script>
        <![CDATA[

            public function addListeners():void {
                a.addEventListener(MouseEvent.CLICK, buttonSwitcher);
                b.addEventListener(MouseEvent.CLICK, buttonSwitcher);
            }

            public function buttonSwitcher(event:MouseEvent):void {

                form.defaultButton = (((event.currentTarget as TextInput).id == "a") ? aButton : bButton); 

            }
        ]]>
    </mx:Script>

    <mx:Panel>
        <mx:Form id="form">
            <mx:FormItem label="a" direction="horizontal">
                <mx:TextInput id="a" />
                <mx:Button id="aButton" label="aButton" />
            </mx:FormItem>
            <mx:FormItem label="b" direction="horizontal">
                <mx:TextInput id="b" />
                <mx:Button id="bButton" label="bButton" />
            </mx:FormItem>
        </mx:Form>
    </mx:Panel>
</mx:Application>

1 个答案:

答案 0 :(得分:0)

使用事件的ht ecapture阶段来更改按钮并使用FocusEvent而不是CLICK,然后在通过inputfields使用“tabs”时切换按钮:

private function addListeners():void
{
    a.addEventListener(FocusEvent.FOCUS_IN, buttonSwitcher, true);
    b.addEventListener(FocusEvent.FOCUS_IN, buttonSwitcher, true);
}

public function buttonSwitcher(event:FocusEvent):void 
{
    form.defaultButton = (((event.currentTarget as TextInput).id == "a") ? aButton : bButton); 
}