1.一個最簡單的例子,按鈕點擊事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<fx:Script> <![CDATA[ import
mx.controls.Alert; protected
function
btn_clickHandler(event:MouseEvent): void { var
btn:Button = event.target as
Button; Alert.show(btn.id); } ]]> </fx:Script> <fx:Declarations> <!--
將非可視元素(例如服務、值對象)放在此處 --> </fx:Declarations> <s:Button
x= "132"
y= "179"
label = "按鈕1"
id= "btn1"
click= "btn_clickHandler(event)" /> <s:Button
x= "280"
y= "179"
label = "按鈕2"
id= "btn2"
click= "btn_clickHandler(event)"
/> |
這個例子我添加了兩個按鈕,共用了同一個事件,在事件裏可以通過event.target來判斷到底點擊的是哪一個按鈕。
2.加載圖片的例子,並且允許手工設置圖片的寬和高
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<fx:Script> <![CDATA[ protected
function
button1_clickHandler(event:MouseEvent): void { img.width
= Number (imgWidth.text); img.height
= Number (imgHeight.text); } ]]> </fx:Script> <fx:Declarations> <!--
將非可視元素(例如服務、值對象)放在此處 --> </fx:Declarations> <mx:Image
x= "106"
y= "162"
source= "@Embed('/test/1.jpg')"
width= "211"
height= "182"
id= "img" /> <s:Button
x= "220"
y= "38"
label = "設置"
click= "button1_clickHandler(event)" /> <s:TextInput
x= "109"
y= "38"
width= "35"
id= "imgWidth" /> <s:TextInput
x= "162"
y= "38"
width= "35"
id= "imgHeight" /> |
我把1.jpg放到test包裏了,寫路徑的話就需要這麼寫:@Embed('/test/1.jpg'),不然可以會出現“無法解析用於轉換代碼”的錯誤;
Number()用於將給定值轉換成數字值,用法很簡單;
如果需要讓圖片加載的時候寬度和高度跟圖片原始寬高度一樣,可以這麼寫:<mx:Image x="106" y="162" source="@Embed('/test/1.jpg')" width="100%" height="100%" id="img" autoLoad="true" scaleContent="false"/>
3.加載swf的例子
1
|
<mx:SWFLoader
x= "5"
y= "5"
source= "@Embed('a.swf')"
scaleContent= "false"
autoLoad= "true"
width= "600"
height= "500" /> |
4.調色板的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<fx:Script> <![CDATA[ import
mx.events.ColorPickerEvent; protected
function
colorpicker1_changeHandler(event:ColorPickerEvent): void { text1.setStyle( "color" ,event.color); } ]]> </fx:Script> <s:TextInput
id= "text1"
x= "114"
y= "120"
color= "#FF0000" /> <mx:ColorPicker
x= "250"
y= "120"
change= "colorpicker1_changeHandler(event)" /> |
該例子有一個輸入框,輸入框內文字默認是紅色,可以通過調色板修改輸入框內字體顏色
5.下拉框DropDownList和ComboBox的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<fx:Script> <![CDATA[ import
mx.collections.ArrayCollection; [Bindable] private
var
dp:ArrayCollection = new
ArrayCollection([{id: 1 ,name: '蘋果' },{id: 2 ,name: '梨' },{id: 3 ,name: '香蕉' }]); protected
function
func(item: Object ): String { return
"水果:"
+ item.name; } ]]> </fx:Script> <s:DropDownList
x= "157"
y= "89"
dataProvider= "{dp}"
labelField= "name"
selectedIndex= "0" /> <s:DropDownList
x= "157"
y= "159"
dataProvider= "{dp}"
labelFunction= "func"
selectedIndex= "0" /> <s:ComboBox
x= "291"
y= "89"
dataProvider= "{dp}"
labelField= "name"
selectedIndex= "0" /> <s:ComboBox
x= "291"
y= "159"
dataProvider= "{dp}"
labelFunction= "func"
selectedIndex= "0" /> |
使用DropDownList控件,下拉框是隻讀的,使用ComboBox控件的話,下拉框的內容可以輸入;
使用[Bindable],表示定義的數據可以做爲數據源引用,在Flex4中不添加該內容也沒有問題,只是代碼會有警告提示,建議使用
6.使用控件MenuBar添加一個菜單,並且在子菜單點擊時執行事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<fx:Script> <![CDATA[ import
mx.controls.Alert; import
mx.events.MenuEvent; protected
function
menubar1_itemClickHandler(event:MenuEvent): void { Alert.show(event.item.@ label ); } ]]> </fx:Script> <mx:MenuBar
x= "118"
y= "124"
labelField= "@label"
itemClick= "menubar1_itemClickHandler(event)" > <mx:dataProvider> <mx:XMLListCollection> <fx:XMLList
xmlns= "" > <menu
label = "aa" > <item
label = "aa1"
/> <item
label = "aa2"
/> </menu> <menu
label = "bb" > <item
label = "bb1"
/> <item
label = "bb2"
/> </menu>
</fx:XMLList> </mx:XMLListCollection> </mx:dataProvider> </mx:MenuBar> |
7.日期控件DateField以及DateChooser的簡單用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<fx:Script> <![CDATA[ import
mx.events.CalendarLayoutChangeEvent; protected
function
datechooser1_changeHandler(event:CalendarLayoutChangeEvent): void { text1.text
= event.newDate.getFullYear().toString() + "-"
+ (event.newDate.getMonth()+ 1 ).toString()
+ "-"
+ event.newDate.getDate().toString(); } ]]> </fx:Script> <mx:DateField
x= "110"
y= "128"
formatString= "YYYY-MM-DD"
/> <mx:DateChooser
x= "374"
y= "128"
change= "datechooser1_changeHandler(event)" /> <s:TextInput
x= "374"
y= "98"
id= "text1" /> |
1.一個最簡單的例子,按鈕點擊事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<fx:Script> <![CDATA[ import
mx.controls.Alert; protected
function
btn_clickHandler(event:MouseEvent): void { var
btn:Button = event.target as
Button; Alert.show(btn.id); } ]]> </fx:Script> <fx:Declarations> <!--
將非可視元素(例如服務、值對象)放在此處 --> </fx:Declarations> <s:Button
x= "132"
y= "179"
label = "按鈕1"
id= "btn1"
click= "btn_clickHandler(event)" /> <s:Button
x= "280"
y= "179"
label = "按鈕2"
id= "btn2"
click= "btn_clickHandler(event)"
/> |
這個例子我添加了兩個按鈕,共用了同一個事件,在事件裏可以通過event.target來判斷到底點擊的是哪一個按鈕。
2.加載圖片的例子,並且允許手工設置圖片的寬和高
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<fx:Script> <![CDATA[ protected
function
button1_clickHandler(event:MouseEvent): void { img.width
= Number (imgWidth.text); img.height
= Number (imgHeight.text); } ]]> </fx:Script> <fx:Declarations> <!--
將非可視元素(例如服務、值對象)放在此處 --> </fx:Declarations> <mx:Image
x= "106"
y= "162"
source= "@Embed('/test/1.jpg')"
width= "211"
height= "182"
id= "img" /> <s:Button
x= "220"
y= "38"
label = "設置"
click= "button1_clickHandler(event)" /> <s:TextInput
x= "109"
y= "38"
width= "35"
id= "imgWidth" /> <s:TextInput
x= "162"
y= "38"
width= "35"
id= "imgHeight" /> |
我把1.jpg放到test包裏了,寫路徑的話就需要這麼寫:@Embed('/test/1.jpg'),不然可以會出現“無法解析用於轉換代碼”的錯誤;
Number()用於將給定值轉換成數字值,用法很簡單;
如果需要讓圖片加載的時候寬度和高度跟圖片原始寬高度一樣,可以這麼寫:<mx:Image x="106" y="162" source="@Embed('/test/1.jpg')" width="100%" height="100%" id="img" autoLoad="true" scaleContent="false"/>
3.加載swf的例子
1
|
<mx:SWFLoader
x= "5"
y= "5"
source= "@Embed('a.swf')"
scaleContent= "false"
autoLoad= "true"
width= "600"
height= "500" /> |
4.調色板的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<fx:Script> <![CDATA[ import
mx.events.ColorPickerEvent; protected
function
colorpicker1_changeHandler(event:ColorPickerEvent): void { text1.setStyle( "color" ,event.color); } ]]> </fx:Script> <s:TextInput
id= "text1"
x= "114"
y= "120"
color= "#FF0000" /> <mx:ColorPicker
x= "250"
y= "120"
change= "colorpicker1_changeHandler(event)" /> |
該例子有一個輸入框,輸入框內文字默認是紅色,可以通過調色板修改輸入框內字體顏色
5.下拉框DropDownList和ComboBox的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<fx:Script> <![CDATA[ import
mx.collections.ArrayCollection; [Bindable] private
var
dp:ArrayCollection = new
ArrayCollection([{id: 1 ,name: '蘋果' },{id: 2 ,name: '梨' },{id: 3 ,name: '香蕉' }]); protected
function
func(item: Object ): String { return
"水果:"
+ item.name; } ]]> </fx:Script> <s:DropDownList
x= "157"
y= "89"
dataProvider= "{dp}"
labelField= "name"
selectedIndex= "0" /> <s:DropDownList
x= "157"
y= "159"
dataProvider= "{dp}"
labelFunction= "func"
selectedIndex= "0" /> <s:ComboBox
x= "291"
y= "89"
dataProvider= "{dp}"
labelField= "name"
selectedIndex= "0" /> <s:ComboBox
x= "291"
y= "159"
dataProvider= "{dp}"
labelFunction= "func"
selectedIndex= "0" /> |
使用DropDownList控件,下拉框是隻讀的,使用ComboBox控件的話,下拉框的內容可以輸入;
使用[Bindable],表示定義的數據可以做爲數據源引用,在Flex4中不添加該內容也沒有問題,只是代碼會有警告提示,建議使用
6.使用控件MenuBar添加一個菜單,並且在子菜單點擊時執行事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<fx:Script> <![CDATA[ import
mx.controls.Alert; import
mx.events.MenuEvent; protected
function
menubar1_itemClickHandler(event:MenuEvent): void { Alert.show(event.item.@ label ); } ]]> </fx:Script> <mx:MenuBar
x= "118"
y= "124"
labelField= "@label"
itemClick= "menubar1_itemClickHandler(event)" > <mx:dataProvider> <mx:XMLListCollection> <fx:XMLList
xmlns= "" > <menu
label = "aa" > <item
label = "aa1"
/> <item
label = "aa2"
/> </menu> <menu
label = "bb" > <item
label = "bb1"
/> <item
label = "bb2"
/> </menu>
</fx:XMLList> </mx:XMLListCollection> </mx:dataProvider> </mx:MenuBar> |
7.日期控件DateField以及DateChooser的簡單用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<fx:Script> <![CDATA[ import
mx.events.CalendarLayoutChangeEvent; protected
function
datechooser1_changeHandler(event:CalendarLayoutChangeEvent): void { text1.text
= event.newDate.getFullYear().toString() + "-"
+ (event.newDate.getMonth()+ 1 ).toString()
+ "-"
+ event.newDate.getDate().toString(); } ]]> </fx:Script> <mx:DateField
x= "110"
y= "128"
formatString= "YYYY-MM-DD"
/> <mx:DateChooser
x= "374"
y= "128"
change= "datechooser1_changeHandler(event)" /> <s:TextInput
x= "374"
y= "98"
id= "text1" /> |