Flex - 事件处理


Flex 使用事件概念根据应用程序内的状态或用户交互将数据从一个对象传递到另一个对象。

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

事件具有以下三个关键属性

先生编号 属性及描述
1

类型

类型说明刚刚发生了什么类型的事件。这可能是单击、初始化、鼠标悬停、更改等。实际值将由 MouseEvent.CLICK 等常量表示。

2

目标

Event 的target属性是对生成事件的组件的对象引用。如果单击 id 为 clickMeButton 的 Button,则该单击事件的目标将是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 事件阶段

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

描述
1 按照Flex - 创建应用程序一章中的说明,在com.tutorialspoint.client包下创建一个名为HelloWorld的项目。
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 事件处理