Flex使用事件的概念將數據從一個對象傳遞到另一個對象,這取決於應用程式中的狀態或用戶交互。
ActionScript有一個泛型的Event類,它定義了處理事件所需的許多功能。每次在Flex應用程式中發生事件時,都會創建事件類層次結構中的三種類型的對象。
事件具有以下三個關鍵屬性
Sr.No | Property & Description |
---|---|
1 | 類型 類型表示剛剛發生的事件類型。這可以是click、initialize、mouseover、change等。實際值將由MouseEvent.click等常量表示。 |
2 | 目標 Event的target屬性是對生成事件的組件的對象引用。如果單擊id爲clickMeButton的按鈕,則該單擊事件的目標將是clickMeButton |
3 | 當前目標 currentTarget屬性改變容器層次結構。它主要處理事件流。 |
Event Flow Phases
一個事件經過三個階段尋找事件處理程序。
Sr.No | Phase & Description |
---|---|
1 | capture/ 在捕獲階段,程序將開始從外部(或頂部)父級到最內部的父級查找事件處理程序。捕獲階段在觸發事件的對象的父級停止。 |
2 | 目標 在目標階段,會檢查觸發事件的組件是否有事件處理程序。 |
3 | 氣泡 氣泡階段與捕獲階段相反,從目標組件的父級向上,通過結構反向工作。 |
考慮下面的應用程式代碼−
<?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" xmlns:mx = "library://ns.adobe.com/flex/mx width = "100%" height = "100%" minWidth = "500" minHeight = "500" > <s:Panel> <s:Button id = "clickMeButton" label = "Click Me!" click = "doAction( );" /> </s:Panel> </s:Application>
當用戶單擊按鈕時,他或她也會單擊面板和應用程式。
事件將經歷三個階段來查找事件處理程序分配。
Let us follow the steps below to test event handing in a Flex application −
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a package com.tutorialspoint.client as explained in the Flex - Create Application chapter. |
2 | Modify HelloWorld.mxml as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to make sure business logic is working as per the requirements. |
Following is the content of the modified mxml file src/com.tutorialspoint/HelloWorld.mxml.
<?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" xmlns:mx = "library://ns.adobe.com/flex/mx width = "100%" height = "100%" minWidth = "500" minHeight = "500"> <fx:Style source = "/com/tutorialspoint/client/Style.css" /> <fx:Script> <![CDATA[ protected function reportEvent(event:MouseEvent):void { var target:String = event.target.id; var currentTarget:String = event.target.id; var eventPhase: String; if(event.target is Button) { var button:Button = event.target as Button; target = button.label + " Button"; } else if(event.target is HGroup) { var hGroup:HGroup = event.target as HGroup; target = hGroup.id + " HGroup"; } else if(event.target is Panel) { var panel:Panel = event.target as Panel; target = panel.id + " Panel"; } if(event.currentTarget is Button) { var button1:Button = event.currentTarget as Button; currentTarget = button1.label + " Button"; } else if(event.currentTarget is HGroup) { var hGroup1:HGroup = event.currentTarget as HGroup; currentTarget = hGroup1.id + " HGroup"; } else if(event.currentTarget is Panel) { var panel1:Panel = event.currentTarget as Panel; currentTarget = panel1.id + " Panel"; } var eventPhaseInt:uint = event.eventPhase; if(eventPhaseInt == EventPhase.AT_TARGET) { eventPhase = "Target"; } else if(eventPhaseInt == EventPhase.BUBBLING_PHASE) { eventPhase = "Bubbling"; } else if(eventPhaseInt == EventPhase.CAPTURING_PHASE) { eventPhase = "Capturing"; } reports.text += " Target: " + target + "\n currentTarget: " + currentTarget + "\n Phase: " + eventPhase + "\n----------\n"; } ]]> </fx:Script> <s:BorderContainer width = "630" height = "480" id = "mainContainer" styleName = "container"> <s:VGroup width = "100%" height = "100%" gap = "10" horizontalAlign = "center" verticalAlign = "middle"> <s:Label id = "lblHeader" text = "Event Handling Demonstration" fontSize = "40" color = "0x777777" styleName = "heading" /> <s:Panel id = "parentPanel" title = "Main Parent" click = "reportEvent(event)" width = "500" height = "100" includeInLayout = "true" visible = "true"> <s:layout> <s:VerticalLayout gap = "10" verticalAlign = "middle" horizontalAlign = "center" /> </s:layout> <s:HGroup id = "mainHGroup" click = "reportEvent(event)"> <s:Button label = "Click Me" click = "reportEvent(event)" /> </s:HGroup> </s:Panel> <s:Panel id = "reportPanel" title = "Events" width = "500" height = "230"> <mx:Text id = "reports" /> </s:Panel> </s:VGroup> </s:BorderContainer> </s:Application>
完成所有更改後,讓我們以正常模式編譯並運行應用程式,就像我們在flex-create application一章中所做的那樣。如果您的應用程式一切正常,它將產生以下結果:[聯機試用]