2013-05-21 52 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没有任何幻想或重要。它可能只是一个普通的事件,因为没有特殊的有效载荷。

我已经测试了上面的代码,而这一切的作品,但也有一定边界条件,如果它是在一个更大的系统中应该加以解决,其他的怪事。

相关问题