存儲和驗(yàn)證數(shù)據(jù)
你可以使用數(shù)據(jù)模型來存儲特定數(shù)據(jù),數(shù)據(jù)模型是一個可以提供存儲數(shù)據(jù)屬性和包含附加方法的
AS
對象。申明一個簡單的沒有任何方法的數(shù)據(jù)模型可以使用
<mx:Model>
或
<mx:XML>
標(biāo)記,你還可以使用驗(yàn)證組件驗(yàn)證存儲數(shù)據(jù)的有效性。
Flex
包含了一套標(biāo)準(zhǔn)的數(shù)據(jù)驗(yàn)證組件,當(dāng)然你也可以創(chuàng)建自己的驗(yàn)證組件。
下面的例子顯示了一個簡單的數(shù)據(jù)驗(yàn)證。
1.
<
mx
:
Application
?
xmlns
:
mx
=
'http://www.macromedia.com/2003/mxml'
?
>
?
2.
<
mx
:
Panel
?
title
=
'My
?
Application'
?
marginTop
=
'10'
?
marginBottom
=
'10'
?
3.
marginLeft
=
'10'
?
marginRight
=
'10'
?
>
?
4.
<
mx
:
TextInput
?
id
=
'homePhoneInput'
?
text
=
'這不是一個有效的電話號碼'
/>
?
5.
<
mx
:
TextInput
?
id
=
'cellPhoneInput'
?
text
=
'(999)999-999'
/>
6.
<
mx
:
TextInput
?
id
=
'emailInput'
?
text
=
'me@somewhere.net'
/>
7.
</
mx
:
Panel
>
?
8.
<
mx
:
Model
?
id
=
'contact'
>
?
9.
<
homePhone
>{
?
homePhoneInput
.
text
?
}</
homePhone
>
10.?
<
cellPhone
>{
?
cellPhoneInput
.
text
?
}</
cellPhone
>
11.?
<
email
>{
?
emailInput
.
text
?
}</
email
>
12.?
</
mx
:
Model
>
13.?
<
mx
:
PhoneNumberValidator
?
field
=
'contact.homePhone'
/>
14.?
<
mx
:
PhoneNumberValidator
?
field
=
'contact.cellPhone'
/>
15.?
<
mx
:
EmailValidator
?
field
=
'contact.email'
/>
16.?
</
mx
:
Application
>
格式化數(shù)據(jù)
除了進(jìn)行數(shù)據(jù)驗(yàn)證之外,格式化輸入的數(shù)據(jù)也是經(jīng)常需要用到的。
Flex
一樣包含了一套用于數(shù)據(jù)格式化的組件,下面的例子對郵編進(jìn)行格式化處理:
1.
<
mx
:
Application
?
xmlns
:
mx
=
'http://www.macromedia.com/2003/mxml'
>
2.
<
mx
:
ZipCodeFormatter
?
id
=
'ZipCodeDisplay'
?
formatString
=
'#####-####'
?
/>
3
<
mx
:
Script
>
4.
<![
CDATA
[
5.
var
?
storedZipCode
=
123456789
;
6.
]]>
7.
</
mx
:
Script
>
8.
<
mx
:
Panel
?
title
=
'My
?
Application'
?
marginTop
=
'10'
?
marginBottom
=
'10'
?
marginLeft
=
'10'
?
marginRight
=
'10'
?
>
9.
<
mx
:
TextInput
?
text
=
''
?
/>
10.?
</
mx
:
Panel
>
11.?
</
mx
:
Application
>
常用的數(shù)據(jù)格式化還有對日期的格式化處理:
: NumberFormatter
數(shù)字格式化
2
: CurrencyFormatter
貨幣格式化
3
: PhoneFormatter
電話號碼格式化
4
: ZipCodeFormatter
郵編格式化
5
: DateFormatter
日期格式化
6
: SwitchSymbolFormatter
創(chuàng)建自定義格式
使用樣式表
還可以使用
<mx:Style>
標(biāo)記表來定義
Flex
組件的樣式表。
1.
<
mx
:
Application
?
xmlns
:
mx
=
'http://www.macromedia.com/2003/mxml'
>
2.
<
mx
:
Style
>
3.
.
myclass
?
{
??
color
:
?
Red
??
}
?
/*
?
class
?
selector
?
*/
4.
Button
?
{
??
font
-
size
:
?
18pt
?
}
?
/*
?
type
?
selector
?
*/
5.
</
mx
:
Style
>
?
6.
<
mx
:
Panel
?
title
=
'My
?
Application'
?
marginTop
=
'10'
?
marginBottom
=
'10'
7.
marginLeft
=
'10'
?
marginRight
=
'10'
?
>
8.
<
mx
:
Button
?
styleName
=
'myclass'
?
label
=
'This
?
is
?
red
?
18
?
point
?
text.'
/>
9.
</
mx
:
Panel
>
10.?
</
mx
:
Application
>
?
使用效果
可以對組件使用過渡效果,效果往往是在事件觸發(fā)后產(chǎn)生,如鼠標(biāo)單擊、組件失去焦點(diǎn)和組件消失等。
Flex
專門提供了一套內(nèi)置的效果組件。下面看一個例子:
<
mx
:
Application
?
xmlns
:
mx
=
'http://www.macromedia.com/2003/mxml'
>?????
<mx:Panel?title='My?Application'?marginTop='10'?marginBottom='10'?marginLeft='10'?marginRight='10'?>
3.
<
mx
:
Button
?
id
=
'myButton'
?
mouseOverEffect
=
'Zoom'
?
/>
4.
</
mx
:
Panel
>
5.
</
mx
:
Application
>
使用
MXML
組件
可以使用
MXML
文件定義自己的組件或者定義已有組件的組合,并通過
<local:
自定義組件名
/>
的方式調(diào)用。
如
? 1.
<!--
?
MyComboBox
.
mxml
?
-->
?
2.
<
mx
:
VBox
?
xmlns
:
mx
=
'http://www.macromedia.com/2003/mxml'
>
?
3.
<
mx
:
ComboBox
?
>
?
4.
<
mx
:
dataProvider
>
?
5.
<
mx
:Array>
?
6.
<
mx
:
String
>
Dogs
</
mx
:
String
>
?
7.
<
mx
:
String
>
Cats
</
mx
:
String
>
?
8.
<
mx
:
String
>
Mice
</
mx
:
String
>
?
9.
</
mx
:Array>
.?
</
mx
:
dataProvider
>
11.?
</
mx
:
ComboBox
>
12.?
</
mx
:
VBox
>
注意,
MXML
組件文件并不需要
<mx:Application>
標(biāo)記,更復(fù)雜的自定義組件可以使用
JSP
或其它語言動態(tài)生成。
調(diào)用自定義組件的應(yīng)用程序文件格式如下:
<!--
?
MyApplication
.
mxml
?
-->
2.
<
mx
:
Application
?
xmlns
:
mx
=
'http://www.macromedia.com/2003/mxml'
?
xmlns
:
local
=
'*'
>
3.
<
mx
:
Panel
?
title
=
'My
?
Application'
?
marginTop
=
'10'
?
marginBottom
=
'10'
?
marginLeft
=
'10'
?
marginRight
=
'10'
?
>
4.
<!--
?
調(diào)用MyComboBox組件
?
-->
5.
<
local
:
MyComboBox
/>
6.
</
mx
:
Panel
>
7.
</
mx
:
Application
>
posted on 2007-01-12 11:45
???MengChuChen 閱讀(168)
評論(0) 編輯 收藏 所屬分類:
flex2.0