Flex 给出是/否框以取消选择复选框

发布于 2024-12-17 03:04:37 字数 1222 浏览 0 评论 0原文

我想在用户取消选择复选框时向他们显示一个确认框。我的代码可以工作,尽管我认为它有点破解。我听到复选框单击的声音,然后显示警报。根据结果​​,我将复选框设置为再次选中。

我的代码是

<?xml version="1.0"?>
<s:NavigatorContent xmlns:fx="http://ns.adobe.com/mxml/2009"   xmlns:s="library://ns.adobe.com/flex/spark" >
<fx:Script><![CDATA[
    import mx.controls.Alert;
    import mx.events.CloseEvent;

    private function handleCheckBoxChange(e:Event):void {
        if (!CheckBox(e.target).selected) {
            Alert.show("Are you sure you want to deselect?", "Confirm",
                Alert.YES | Alert.NO, null, handleAlert, null, Alert.YES);
        }
    }

    public function handleAlert(event:CloseEvent):void {
        if (event.detail == Alert.YES) {
            trace("yes clicked");
        }
        else if (event.detail == Alert.NO) {
            cb1.selected = true;
            trace("no clicked");
        }
    }
    ]]></fx:Script>

    <s:CheckBox id="cb1" label="cb1" click="handleCheckBoxChange(event)"/>
</s:NavigatorContent>

有两件事我不喜欢这个

  1. 代码是特定于 cb1 的,不能重用于其他复选框
  2. 当警报显示时,复选框被取消选中。然后,当用户单击“否”时,该复选框将再次被选中。

我理想的情况是,如果用户在警报框中单击“否”,则停止取消选中事件。在Flex中可以拦截这个吗?

谢谢

I want to show the user a confirmation box when they deselect a checkbox. My code works although I think it is a bit of a hack. I listen to the checkbox click and then show the alert. Depending on the result I then set the checkbox to be checked again.

My code is

<?xml version="1.0"?>
<s:NavigatorContent xmlns:fx="http://ns.adobe.com/mxml/2009"   xmlns:s="library://ns.adobe.com/flex/spark" >
<fx:Script><![CDATA[
    import mx.controls.Alert;
    import mx.events.CloseEvent;

    private function handleCheckBoxChange(e:Event):void {
        if (!CheckBox(e.target).selected) {
            Alert.show("Are you sure you want to deselect?", "Confirm",
                Alert.YES | Alert.NO, null, handleAlert, null, Alert.YES);
        }
    }

    public function handleAlert(event:CloseEvent):void {
        if (event.detail == Alert.YES) {
            trace("yes clicked");
        }
        else if (event.detail == Alert.NO) {
            cb1.selected = true;
            trace("no clicked");
        }
    }
    ]]></fx:Script>

    <s:CheckBox id="cb1" label="cb1" click="handleCheckBoxChange(event)"/>
</s:NavigatorContent>

There are two things I do not like about this

  1. the code is specific to cb1 and cannot be reused for other checkboxes
  2. the checkbox is deselected when the alert shows. Then when the user clicks no the checkbox is selected again.

What I ideally want is to stop the uncheck event if the user clicks no on the alert box. Is it possible to intercept this in Flex?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半暖夏伤 2024-12-24 03:04:37

解决您的两个问题的一种解决方案:创建一个扩展 CheckBox 的自定义类。
您不能在 click 事件中使用 e.stopImmediatePropagation(),因为您的事件是稍后添加的。然而,如果你追踪,你可以看到在ToggleButtonBase(它是CheckBox的父级)中包含一个名为buttonReleased的受保护函数;此函数将对selected值进行更改并调度change事件。您在新类中所需要做的就是覆盖此函数。

public class AlertCheckBox extends CheckBox
    {
        public function AlertCheckBox()
        {
            super();
        }

        override protected function buttonReleased():void
        {
            if (selected) {
                Alert.show("Are you sure you want to deselect?", "Confirm",
                    Alert.YES | Alert.NO, null, handleAlert, null, Alert.YES);
                return;
            }

            super.buttonReleased();
        }

        public function handleAlert(event:CloseEvent):void {
            if (event.detail == Alert.YES) {
                selected = false;
            }
        }
    }

One solution for your two problems: Create a custom class that extends CheckBox.
You cannot use e.stopImmediatePropagation() in click Event because yours is added later. However, if you trace up, you can see in ToggleButtonBase (which is parent of CheckBox) contains a protected function named buttonReleased; this function will do the change for selected value and dispatch a change Event. All you need to do in your new class is overriding this function.

public class AlertCheckBox extends CheckBox
    {
        public function AlertCheckBox()
        {
            super();
        }

        override protected function buttonReleased():void
        {
            if (selected) {
                Alert.show("Are you sure you want to deselect?", "Confirm",
                    Alert.YES | Alert.NO, null, handleAlert, null, Alert.YES);
                return;
            }

            super.buttonReleased();
        }

        public function handleAlert(event:CloseEvent):void {
            if (event.detail == Alert.YES) {
                selected = false;
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文