2013-05-21 56 views
0

有沒有辦法聽這個?我可以很容易監聽點擊,其選擇單選按鈕,但我似乎無法找到一種方法來聽時,單選按鈕已經被取消了。Flex Spark RadioButton取消選中的事件?

任何想法?

謝謝!

在Flex2天
+1

我可能會嘗試收聽RadioButtonGroup的itemClick或Change事件:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/spark/components/RadioButtonGroup.html#event:itemClick – JeffryHouser

+0

我同意,你也可以爲單個單選按鈕。只需檢查它是否已在事件處理程序中被選中或取消選定。儘管你可能會也可能不會得到誤報和否定。如果他們只是點擊了文字或其他內容,而沒有真正改變這個值,我仍然想這會觸發點擊事件,所以要小心。 – Panzercrisis

+0

我能夠使用FlexEvent.UPDATE_COMPLETE的單選按鈕,然後檢查所選擇的價值。 – user1513171

回答

0

返回,當一個單選按鈕被取消引發了「改變」事件。然而,Flex 3中這個小便利消失了,出於某種原因,我不相信我們會得到任何形式的替換事件。

在RadioButtonGroup級別處理您的事件會很好,除非有時候您真的想在單選按鈕級別處理事件 - 特別是如果您希望通過通過數據提供程序進行交互一個正在繪製單選按鈕的itemRenderer。

方便地,我可以使用一些樣板代碼作爲RadioButton和RadioButtonGroup的插件替換,它們在單選按鈕級別提供「取消選擇」事件。這裏是SmartRadioButton,首先:

<?xml version="1.0" encoding="utf-8"?> 
<s:RadioButton xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx"> 
    <fx:Script> 
     <![CDATA[ 
      import events.SmartRadioButtonEvent; 

      public function notifyDeselect():void { 
       dispatchEvent(new SmartRadioButtonEvent('deselect')); 
      } 
     ]]> 
    </fx:Script> 

    <fx:Metadata> 
     [Event(name="deselect", type="events.SmartRadioButtonEvent")] 
    </fx:Metadata> 
</s:RadioButton> 

這裏是SmartRadioButtonGroup:

<?xml version="1.0" encoding="utf-8"?> 
<s:RadioButtonGroup xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx" 
        change="selectionChanged();" 
        > 
    <fx:Script> 
     <![CDATA[ 
      import spark.components.RadioButton; 

      import components.SmartRadioButton; 

      protected var oldSelection:SmartRadioButton = null; 

      // Notify our old selection that it has been deselected, and update 
      // the reference to the new selection. 
      public function selectionChanged():void { 
       var newSelection:SmartRadioButton = this.selection as SmartRadioButton; 
       if (oldSelection == newSelection) return; 
       if (oldSelection != null) { 
        oldSelection.notifyDeselect(); 
       } 
       oldSelection = newSelection; 
      } 

      // Override some properties to make sure that we update oldSelection correctly, 
      // in the event of a programmatic selection change. 
      override public function set selectedValue(value:Object):void { 
       super.selectedValue = value; 
       oldSelection = super.selection as SmartRadioButton; 
      } 

      override public function set selection(value:RadioButton):void { 
       super.selection = value; 
       oldSelection = super.selection as SmartRadioButton; 
      } 

     ]]> 
    </fx:Script> 
</s:RadioButtonGroup> 

兩個屬性覆蓋在那裏,以確保我們正確地更新oldSelection,在發生對團隊選擇進行程序化改變。

SmartRadioButtonEvent沒有任何幻想或重要。它可能只是一個普通的事件,因爲沒有特殊的有效載荷。

我已經測試了上面的代碼,而這一切的作品,但也有一定邊界條件,如果它是在一個更大的系統中應該加以解決,其他的怪事。

相關問題