Flex中如何利用StyleManager.getStyleDeclaration()和setStyle()函数动态设置.errorTip样式的例子
By Minidxer | September 27, 2008
在前面Flex中通过设置borderColor样式和.errorTip CSS分离器改变错误信息提示背景颜色(background color)的例子中,我们了解了如何通过设置borderColor样式和.errorTip CSS分离器改变错误信息提示背景颜色(background color)。接下来的例子演示了Flex中如何利用StyleManager.getStyleDeclaration()和setStyle()函数,动态设置.errorTip样式。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application name="errorTip_borderColor_test_2"
- xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white">
- <mx:Script>
- <![CDATA[
- import mx.events.FlexEvent;
- private function comboBox_valueCommit(evt:FlexEvent):void {
- if (comboBox.selectedIndex > -1) {
- var value:String = comboBox.selectedItem.data;
- var cssObj:CSSStyleDeclaration;
- cssObj = StyleManager.getStyleDeclaration(".errorTip");
- cssObj.setStyle("borderColor", value);
- textInput.setStyle("errorColor", value);
- textInput.errorString = errStr;
- } else {
- textInput.errorString = "";
- }
- }
- private function button_click(evt:MouseEvent):void {
- comboBox.selectedIndex = -1;
- }
- ]]>
- </mx:Script>
- <mx:Array id="arr">
- <mx:Object label="Red" data="red" />
- <mx:Object label="Orange" data="haloOrange" />
- <mx:Object label="Yellow" data="yellow" />
- <mx:Object label="Green" data="haloGreen" />
- <mx:Object label="Blue" data="haloBlue" />
- </mx:Array>
- <mx:String id="errStr">Oh noes, an errata!</mx:String>
- <mx:ApplicationControlBar dock="true">
- <mx:Form styleName="plain">
- <mx:FormItem label="errorColor:">
- <mx:ComboBox id="comboBox"
- dataProvider="{arr}"
- prompt="Please select a color:"
- valueCommit="comboBox_valueCommit(event);" />
- </mx:FormItem>
- </mx:Form>
- <mx:Spacer width="100%" />
- <mx:Button label="Deselect ComboBox"
- click="button_click(event);" />
- </mx:ApplicationControlBar>
- <mx:TextInput id="textInput" />
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子
Topics:
Flex |
Tags: borderColor, errorColor, errorTip, getStyleDeclaration, StyleManager