Flex 给出是/否框以取消选择复选框
我想在用户取消选择复选框时向他们显示一个确认框。我的代码可以工作,尽管我认为它有点破解。我听到复选框单击的声音,然后显示警报。根据结果,我将复选框设置为再次选中。
我的代码是
<?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>
有两件事我不喜欢这个
- 代码是特定于 cb1 的,不能重用于其他复选框
- 当警报显示时,复选框被取消选中。然后,当用户单击“否”时,该复选框将再次被选中。
我理想的情况是,如果用户在警报框中单击“否”,则停止取消选中事件。在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
- the code is specific to cb1 and cannot be reused for other checkboxes
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决您的两个问题的一种解决方案:创建一个扩展 CheckBox 的自定义类。
您不能在
click
事件中使用e.stopImmediatePropagation()
,因为您的事件是稍后添加的。然而,如果你追踪,你可以看到在ToggleButtonBase
(它是CheckBox
的父级)中包含一个名为buttonReleased
的受保护函数;此函数将对selected
值进行更改并调度change
事件。您在新类中所需要做的就是覆盖此函数。One solution for your two problems: Create a custom class that extends CheckBox.
You cannot use
e.stopImmediatePropagation()
inclick
Event because yours is added later. However, if you trace up, you can see inToggleButtonBase
(which is parent ofCheckBox
) contains a protected function namedbuttonReleased
; this function will do the change forselected
value and dispatch achange
Event. All you need to do in your new class is overriding this function.