Flex中如何通过设置RadioButtonGroup类的selection属性用ActionScript清除当前选中RadioButton的例子

By Minidxer | February 7, 2009

接下来的例子演示了Flex中如何通过设置RadioButtonGroup类的selection属性,用ActionScript清除当前选中RadioButton。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="middle"
  5.         backgroundColor="white">
  6.  
  7.     <mx:Script>
  8.         <![CDATA[
  9.             import mx.controls.Alert;
  10.  
  11.             private function reset():void {
  12.                 answers.selection = null;
  13.             }
  14.  
  15.             private function check():void {
  16.                 if (answers.selectedValue) {
  17.                     Alert.show(answers.selectedValue.toString());
  18.                 } else {
  19.                     Alert.show("unselected");
  20.                 }
  21.             }
  22.         ]]>
  23.     </mx:Script>
  24.  
  25.     <mx:RadioButtonGroup id="answers" />
  26.  
  27.     <mx:Form backgroundColor="red">
  28.         <mx:FormItem>
  29.             <mx:Text id="question"
  30.                     text="Question..." />
  31.         </mx:FormItem>
  32.         <mx:FormItem>
  33.             <mx:RadioButton id="answer1"
  34.                     group="{answers}"
  35.                     label="Answer 1" />
  36.         </mx:FormItem>
  37.         <mx:FormItem>
  38.             <mx:RadioButton id="answer2"
  39.                     group="{answers}"
  40.                     label="Answer 2" />
  41.         </mx:FormItem>
  42.         <mx:FormItem>
  43.             <mx:RadioButton id="answer3"
  44.                     group="{answers}"
  45.                     label="Answer 3" />
  46.         </mx:FormItem>
  47.         <mx:FormItem>
  48.             <mx:RadioButton id="answer4"
  49.                     group="{answers}"
  50.                     label="Answer 4" />
  51.         </mx:FormItem>
  52.         <mx:FormItem>
  53.             <mx:RadioButton id="answer5"
  54.                     group="{answers}"
  55.                     label="Answer 5" />
  56.         </mx:FormItem>
  57.         <mx:FormItem direction="horizontal">
  58.             <mx:Button label="Check" click="check();" />
  59.             <mx:Button label="Reset" click="reset();" />
  60.         </mx:FormItem>
  61.     </mx:Form>
  62.  
  63. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: RadioButton | No Comments » | 854 views Tags: , , ,

Search Posts