Flex中通过扩展LinkButtonSkin和添加自己的selectedUpSkin,selectedOverSkin,selectedDownSkin,selectedDisabledSkin设置LinkButton控件是否保持鼠标按下状态的例子

By Minidxer | September 7, 2008

一般按钮鼠标点下离开后,按下的状态 接下来的例子演示了Flex中如何通过扩展LinkButtonSkin和添加自己的selectedUpSkin,selectedOverSkin,selectedDownSkin,selectedDisabledSkin,设置LinkButton控件是否保持鼠标按下状态。

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


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

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application name="LinkButton_toggle_test"
  3.         xmlns:mx="http://www.adobe.com/2006/mxml"
  4.         layout="vertical"
  5.         verticalAlign="middle"
  6.         backgroundColor="white">
  7.  
  8.     <mx:ApplicationControlBar dock="true">
  9.         <mx:Form styleName="plain">
  10.             <mx:FormItem label="toggle:">
  11.                 <mx:CheckBox id="toggleCheckBox" />
  12.             </mx:FormItem>
  13.             <mx:FormItem label="selected:">
  14.                 <mx:CheckBox id="selectedCheckBox"
  15.                         selected="{linkButton.selected}" />
  16.             </mx:FormItem>
  17.         </mx:Form>
  18.     </mx:ApplicationControlBar>
  19.  
  20.     <mx:LinkButton id="linkButton"
  21.             label="LinkButton"
  22.             toggle="{toggleCheckBox.selected}"
  23.             selected="{selectedCheckBox.selected}"
  24.             skin="skins.ToggleLinkButtonSkin" />
  25.  
  26. </mx:Application>

下面是ToggleLinkButtonSkin.as的代码:

Download: main.mxml
  1. package {
  2.     import mx.skins.halo.LinkButtonSkin;
  3.  
  4.     public class ToggleLinkButtonSkin extends LinkButtonSkin {
  5.         public function ToggleLinkButtonSkin() {
  6.             super();
  7.         }
  8.  
  9.         override protected function updateDisplayList(w:Number, h:Number):void {
  10.             super.updateDisplayList(w, h);
  11.  
  12.             var cornerRadius:Number = getStyle("cornerRadius");
  13.             var rollOverColor:uint = getStyle("rollOverColor");
  14.             var selectionColor:uint = getStyle("selectionColor");
  15.  
  16.             graphics.clear();
  17.  
  18.             switch (name) {
  19.                 case "upSkin":
  20.                     // Draw invisible shape so we have a hit area.
  21.                     drawRoundRect(
  22.                         0, 0, w, h, cornerRadius,
  23.                         0, 0);
  24.                     break;
  25.  
  26.                 case "selectedUpSkin":
  27.                 case "selectedOverSkin":
  28.                 case "overSkin":
  29.                     drawRoundRect(
  30.                         0, 0, w, h, cornerRadius,
  31.                         rollOverColor, 1);
  32.                     break;
  33.  
  34.                 case "selectedDownSkin":
  35.                 case "downSkin":
  36.                     drawRoundRect(
  37.                         0, 0, w, h, cornerRadius,
  38.                         selectionColor, 1);
  39.                     break;
  40.  
  41.                 case "selectedDisabledSkin":
  42.                 case "disabledSkin":
  43.                     // Draw invisible shape so we have a hit area.
  44.                     drawRoundRect(
  45.                         0, 0, w, h, cornerRadius,
  46.                         0, 0);
  47.                     break;
  48.             }
  49.         }
  50.     }
  51. }
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: Flex | No Comments » | Tags: , , , , ,

Search Posts