2011-11-21 34 views
1

我想在用戶取消選中複選框時向用戶顯示確認框。我的代碼工作雖然我認爲它有點破解。我聽取複選框的點擊,然後顯示警報。根據結果​​我然後設置複選框以再次檢查。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> 

有兩件事情,我不喜歡這個

  1. 代碼具體到CB1和其他複選框
  2. 的複選框取消選中不能重複使用當警報顯示時。然後當用戶點擊否時,再次選擇複選框。

我最想要的是如果用戶在警告框上單擊否,停止取消選中事件。是否有可能在Flex中攔截這個?

謝謝

回答

2

解決您的兩個問題的一個解決方案:創建一個擴展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; 
      } 
     } 
    } 
+0

非常好的回答哈利。謝謝 – RNJ

相關問題