Flex中如何利用StyleManager.isColorName函数检查颜色名字是否合法的例子
By Minidxer | September 17, 2008
接下来的例子演示了Flex中如何利用StyleManager.isColorName函数检查颜色名字是否合法。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <!-- http://blog.flexexamples.com/2007/09/13/checking-whether-a-color-name-is-a-valid-color-in-flex-using-the-stylemanager-class/ -->
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white">
- <mx:Script>
- <![CDATA[
- import mx.styles.StyleManager;
- import mx.utils.StringUtil;
- private function button_click(evt:MouseEvent):void {
- /* Remove leading and trailing whitespace. */
- var str:String = StringUtil.trim(textInput.text);
- var isColor:Boolean = StyleManager.isColorName(str);
- /* If it is a valid color, set the background color
- and remove the error string, if any. Else, set the
- background color to the constant NOT_A_COLOR from
- the StyleManager class and set the error string. */
- if (isColor) {
- box.setStyle("backgroundColor", str);
- textInput.errorString = "";
- } else {
- box.setStyle("backgroundColor", StyleManager.NOT_A_COLOR);
- textInput.errorString = "NOT A COLOR";
- }
- }
- ]]>
- </mx:Script>
- <mx:ApplicationControlBar dock="true">
- <mx:Label text="Color name:" />
- <mx:TextInput id="textInput" />
- <mx:Button label="isColorName()"
- fontFamily="_sans"
- click="button_click(event);" />
- </mx:ApplicationControlBar>
- <mx:Box id="box" width="100%" height="100%" />
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子
Topics:
Flex |
No Comments » |
28 views
Tags: isColorName(), registerColorName(), StringUtil, StyleManager, trim