创建具有 RadioButton 的应用程序

以下过程解释了如何在创作时将 RadioButton 组件添加到应用程序。在此示例中,使用 RadioButton 表示是非问题。RadioButton 的数据显示在 TextArea 中。

创建具有 RadioButton 组件的应用程序:

  1. 创建一个新的 Flash 文件 (ActionScript 3.0) 文档。
  2. 将两个 RadioButton 组件从"组件"面板拖到舞台上。
  3. 选择第一个单选按钮。在"属性"检查器中,为其指定实例名称"yesRb"和组名称"rbGroup"。
  4. 选择第二个单选按钮。在"属性"检查器中,为其指定实例名称"noRb"和组名称"rbGroup"。
  5. 将一个 TextArea 组件从"组件"面板拖到舞台上,并为其指定实例名称 aTa
  6. 打开"动作"面板,在主时间轴中选择第 1 帧,然后输入以下 ActionScript 代码:
    yesRb.label = "Yes";
    yesRb.value = "For";
    noRb.label = "No";
    noRb.value = "Against";
    
    yesRb.move(50, 100);
    noRb.move(100, 100);
    aTa.move(50, 30);
    noRb.addEventListener(MouseEvent.CLICK, clickHandler);
    yesRb.addEventListener(MouseEvent.CLICK, clickHandler);
    
    function clickHandler(event:MouseEvent):void {
        aTa.text = event.target.value;  
    }
    
  7. 选择"控制">"测试影片",运行应用程序。

下面的示例使用 ActionScript 创建三个 RadioButton,分别表示红色、蓝色和绿色,并绘制一个灰色的框。每个 RadioButton 的 value 属性指定与此按钮关联的颜色的十六进制值。当用户单击其中一个 RadioButton 时,clickHandler() 函数将调用 drawBox(),同时传递此 RadioButton 的 value 属性中的颜色值来为此框着色。

使用 ActionScript 创建 RadioButton:

  1. 创建一个新的 Flash 文件 (ActionScript 3.0) 文档。
  2. 将一个 RadioButton 组件拖到"库"面板中。
  3. 打开"动作"面板,在主时间轴中选择第 1 帧,然后输入以下 ActionScript 代码:
    import fl.controls.RadioButton;
    import fl.controls.RadioButtonGroup;
    
    var redRb:RadioButton = new RadioButton();
    var blueRb:RadioButton = new RadioButton();
    var greenRb:RadioButton = new RadioButton();
    var rbGrp:RadioButtonGroup = new RadioButtonGroup("colorGrp");
    
    var aBox:MovieClip = new MovieClip();
    drawBox(aBox, 0xCCCCCC);
    
    addChild(redRb);
    addChild(blueRb);
    addChild(greenRb);
    addChild(aBox);
    
    redRb.label = "Red";
    redRb.value = 0xFF0000;
    blueRb.label = "Blue";
    blueRb.value = 0x0000FF;
    greenRb.label = "Green";
    greenRb.value = 0x00FF00;
    redRb.group = blueRb.group = greenRb.group = rbGrp;
    redRb.move(100, 260);
    blueRb.move(150, 260);
    greenRb.move(200, 260);
    
    rbGrp.addEventListener(MouseEvent.CLICK, clickHandler);
    
    function clickHandler(event:MouseEvent):void {
        drawBox(aBox, event.target.selection.value);  
    }
    
    function drawBox(box:MovieClip,color:uint):void {
                box.graphics.beginFill(color, 1.0);
                box.graphics.drawRect(125, 150, 100, 100);
                box.graphics.endFill();        
    }
    
  4. 选择"控制">"测试影片",运行应用程序。

有关详细信息,请参阅《ActionScript 3.0 语言和组件参考》中的 RadioButton 类。