开发者

How to stop a toggle button from changing state

开发者 https://www.devze.com 2023-01-25 09:08 出处:网络
I\'m trying to stop a toggle button from changing state when clicked, based on some conditions, but I\'m not sure how to do i开发者_如何转开发t. Is it possible?

I'm trying to stop a toggle button from changing state when clicked, based on some conditions, but I'm not sure how to do i开发者_如何转开发t. Is it possible?

I don't want to disable the button, because flex 3 styles don't allow me to get the visual effect I need.

EDIT:

I've tried this but it does not work:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="_init()"          
>
<mx:Button label="toggle" id="button" toggle="true"/>
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;

private function _init():void{

    button.addEventListener(FlexEvent.BUTTON_DOWN, preventDef, true, 50);
    button.addEventListener(MouseEvent.CLICK, preventDef, true, 50);
    button.addEventListener(Event.CHANGE, preventDef, true, 50);
    button.addEventListener(MouseEvent.MOUSE_DOWN, preventDef, true, 50);
    button.addEventListener(MouseEvent.MOUSE_UP, preventDef, true, 50);
}

private function preventDef(e:Event):void{
    e.preventDefault();
}
]]>
</mx:Script>
</mx:Application>


If it helps, you can use checkbox to get a toggling effect. However, if you insist on using a toggle button for this matter, you need to register a change event for your button, then, after your calculations call

event.preventDefault(); 

method on your MouseEvent object to prevent it from being toggled or not.


Subclass ToggleButton, override method buttonReleased:

override protected function buttonReleased():void {
    if(dispatchEvent(new FlexEvent(FlexEvent.CHANGING, false, true)))
        super.buttonReleased();
}

It's important to set dispatched event as cancelable.

To your button add two listeners. Event.CHANGE and FlexEvent.CHANGING. For CHANGING handler do your calculations and call preventDefault if needed.

private function verifyChange(e:FlexEvent):void {
    if (shouldPreventToggle())
        e.preventDefault();
}


If you don't need a toggle button, then why not use a regular button?

0

精彩评论

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