开发者

default button switcher by using event listener

开发者 https://www.devze.com 2023-03-23 13:58 出处:网络
I tried to make the nearest button to the clicked text input default. For this purpose, I wrote the below code.

I tried to make the nearest button to the clicked text input default. For this purpose, I wrote the below code.

Firstly, why is my buttonSwitcher function following behind the MouseEvent.CLICK ? Secondly, Is there better way to do this ?

Thanks in advance

<?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>


use ht ecapture phase of the event to change the buttons and maybe use the FocusEvent instead of CLICK, then you also switch buttons when use "tabs" through inputfields:

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); 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消