设置state 的name属性的值 ,在其他组件利用里该name的值控制state .
state状态的改变时通过 currentState 属性来改变的。
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:states>
<s:State name="One"/>
<s:State name="Two"/>
<s:State name="Three"/>
</s:states>
<s:HGroup
horizontalCenter="0">
<s:Button
label="One"
click="this.currentState='One'"/>
<s:Button
label="Two"
click="this.currentState='Two'"/>
<s:Button
label="Three"
click="this.currentState='Three'"/>
</s:HGroup>
</s:Application>
上面的程序只是框架 ,现在我们来分析一下。 在flex3里,state的属性值只能在<s:state>里设置,但在flex4里, state的属性也可以再其他组件中设置 。如下面的 text.One 里的One 就是就是state的name属性的值. 后面的一个One 是输入的内容,可以任意设定 。
<s:Label
text.One = "One"
text.Two = "Two"
text.Three = "Three" />
下面是例子
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:states>
<s:State name="One"/>
<s:State name="Two"/>
<s:State name="Three"/>
</s:states>
<s:HGroup
horizontalCenter="0">
<s:Button
label="One"
click="this.currentState='One'"/>
<s:Button
label="Two"
click="this.currentState='Two'"/>
<s:Button
label="Three"
click="this.currentState='Three'"/>
</s:HGroup>
<s:Label
text.One = "One"
text.Two = "Two"
text.Three = "Three"
horizontalCenter="0"
top="50"
fontSize="40"
/>
</s:Application>
上面的程序在<s:Label> 里就用到state的name属性值
下面我们加入 面板panel:
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:states>
<s:State name="One"/>
<s:State name="Two"/>
<s:State name="Three"/>
</s:states>
<s:Panel
resizeEffect="Resize"
title.One="One"
title.Two="Two"
title.Three="Three"
width.One="800"
width.Two="200"
width.Three="400"
height.One="400"
height.Two="600"
height.Three="200"
horizontalCenter="0"
verticalCenter="0"
>
<s:Label
text.One="One"
text.Two="Two"
text.Three="Three"
horizontalCenter="0"
verticalCenter="0"
fontSize="40"
/>
</s:Panel>
<s:HGroup
horizontalCenter="0">
<s:Button
label="One"
enabled.One="false"
click="this.currentState='One'"/>
<s:Button
label="Two"
enabled.Two="false"
click="this.currentState='Two'"/>
<s:Button
label="Three"
enabled.Three="false"
click="this.currentState='Three'"/>
</s:HGroup>
</s:Application>
从<s:panel > 里可以看出(如:title.One="One") 我们可以用其他组件的属性加上点语法再加上state的name属性的值
(即:property.name'sValue="property相关的值") 来控制state .