Flex - 事件处理



Flex 使用事件的概念将数据从一个对象传递到另一个对象,具体取决于应用程序中的状态或用户交互。

ActionScript 具有一个通用的Event类,该类定义了处理事件所需的大部分功能。每次 Flex 应用程序中发生事件时,都会创建来自 Event 类层次结构的三种类型的对象。

Event 具有以下三个关键属性

序号 属性及描述
1

类型

type 属性说明刚刚发生了什么类型的事件。这可能是 click、initialize、mouseover、change 等。实际值将由常量表示,例如 MouseEvent.CLICK。

2

目标

Event 的target 属性是对生成事件的组件的对象引用。如果您单击 ID 为 clickMeButton 的按钮,则该单击事件的目标将是 clickMeButton

3

当前目标

currentTarget 属性会随容器层次结构而变化。它主要处理事件流。

事件流阶段

事件会经过三个阶段,寻找事件处理程序。

序号 阶段及描述
1

捕获

在捕获阶段,程序将从外部(或顶部)父级开始查找事件处理程序,一直到最内部的父级。捕获阶段在触发事件的对象的父级处停止。

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>

当用户单击按钮时,他或她也单击了面板和应用程序。

事件经过三个阶段寻找事件处理程序分配。

Flex event phases

让我们按照以下步骤在 Flex 应用程序中测试事件处理:

步骤 描述
1 创建一个名为HelloWorld的项目,放在com.tutorialspoint.client包下,如Flex - 创建应用程序章节所述。
2 修改HelloWorld.mxml,如下所述。保持其余文件不变。
3 编译并运行应用程序,以确保业务逻辑按要求工作。

以下是修改后的 mxml 文件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 - 创建应用程序章节中一样,在正常模式下编译并运行应用程序。如果应用程序一切正常,它将产生以下结果:[ 在线尝试 ]

Flex Event Handling
广告