Flex中如何利用StyleManager.isColorName函数检查颜色名字是否合法的例子

By Minidxer | September 17, 2008

接下来的例子演示了Flex中如何利用StyleManager.isColorName函数检查颜色名字是否合法。

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


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

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- http://blog.flexexamples.com/2007/09/13/checking-whether-a-color-name-is-a-valid-color-in-flex-using-the-stylemanager-class/ -->
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  4.         layout="vertical"
  5.         verticalAlign="middle"
  6.         backgroundColor="white">
  7.  
  8.     <mx:Script>
  9.         <![CDATA[
  10.             import mx.styles.StyleManager;
  11.             import mx.utils.StringUtil;
  12.  
  13.             private function button_click(evt:MouseEvent):void {
  14.                 /* Remove leading and trailing whitespace. */
  15.                 var str:String = StringUtil.trim(textInput.text);
  16.                 var isColor:Boolean = StyleManager.isColorName(str);
  17.  
  18.                 /* If it is a valid color, set the background color
  19.                    and remove the error string, if any. Else, set the
  20.                    background color to the constant NOT_A_COLOR from
  21.                    the StyleManager class and set the error string. */
  22.                 if (isColor) {
  23.                     box.setStyle("backgroundColor", str);
  24.                     textInput.errorString = "";
  25.                 } else {
  26.                     box.setStyle("backgroundColor", StyleManager.NOT_A_COLOR);
  27.                     textInput.errorString = "NOT A COLOR";
  28.                 }
  29.             }
  30.         ]]>
  31.     </mx:Script>
  32.  
  33.     <mx:ApplicationControlBar dock="true">
  34.         <mx:Label text="Color name:" />
  35.         <mx:TextInput id="textInput" />
  36.         <mx:Button label="isColorName()"
  37.                 fontFamily="_sans"
  38.                 click="button_click(event);" />
  39.     </mx:ApplicationControlBar>
  40.  
  41.     <mx:Box id="box" width="100%" height="100%" />
  42.  
  43. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

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

Search Posts