Adobe
产品
Acrobat
Creative Cloud
创意套装
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
更多产品
解决方案
数字营销
数字媒体
教育
金融服务业
政府部门
网页体验管理
更多解决方案
学习帮助下载公司
商店
在线商店
批量许可
查找经销商
搜索
 
信息 登录
欢迎,我的支持
我的帐户
注销
为何登录?登录后可以管理您的帐户,访问试用版下载、产品扩展和社区区域等。
Adobe
产品 分类 购买   搜索  
解決方案 公司
学习
登录 注销 我的货物 我的支持
Date Date
Qty:
Subtotal
Checkout
Adobe 开发者中心 / Flex 开发人员中心 / Flex 快速入门 /

Handling events

by Adobe

Adobe logo

Created

22 March 2010

页面工具

在 Facebook 上共享
在 Twitter 上共享
在 LinkedIn 上共享
书签
打印

Tags

要求

用户级别

全部

Adobe Flex applications are event-driven. Events let a programmer know when the user interacts with an interface component, and also when important changes occur in the appearance or life cycle of a component, such as the creation or destruction of a component or its resizing.

When an instance of a component dispatches an event, objects that you registered as listeners for that event are notified. You define event listeners, also called event handlers, in ActionScript to process events. You register event listeners for events either in the MXML declaration for the component or in ActionScript.

There are three ways to receive event notifications:

  • Registering an event handler in MXML
  • Creating an inline event handler in the MXML definition
  • Registering an event listener through ActionScript

Registering an event handler in MXML

The first, and most widely used method for getting event notifications is to define an event handler in MXML that is called when an event occurs.

In this example, you define an event handler for the click event of a Button control. When a user clicks the Button control, the event handler sets the text property of the Label control to "Hello, World!".

Tip: Flash Builder 4 provides tooling that generates stubs for event handlers. For more information refer to Generating event handlers in the Adobe Flash Builder 4 Help.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- HandlingEventsEventHandler --> <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="200" height="200"> <fx:Script> <![CDATA[ import flash.events.MouseEvent; private function clickHandler ( event:MouseEvent ):void { myLabel.text = "Hello, World!"; } ]]> </fx:Script> <s:Panel title="My Application" width="100%"> <s:layout> <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/> </s:layout> <s:Label id="myLabel" fontWeight="bold" fontSize="24"/> <s:Button id="myButton" label="Click Me!" click="clickHandler(event);" /> </s:Panel> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Back to top

Creating an inline event handler in the MXML definition

Sometimes the easiest way of responding to events is to define the event handler entirely in the MXML definition of a component. This is known as using an inline event handler.

In the following example, you set the click property of the <s:Button> tag so that it directly sets the text property of the Label control, without calling an event handler method.

Tip: Using inline event handlers may be quick and result in less code, but they also result in code that is harder to read, maintain, and scale. A good rule of thumb is not to include more than one ActionScript statement in an inline event handler. If you must include more complex logic, place it in an ActionScript helper method or in an ActionScript event handler.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- HandlingEventsInlineMethod.mxml --> <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="200" height="200"> <s:Panel title="My Application" width="100%"> <s:layout> <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/> </s:layout> <s:Label id="myLabel" fontWeight="bold" fontSize="24"/> <s:Button id="myButton" label="Click Me!" click="myLabel.text = 'Hello, World!'" /> </s:Panel> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Back to top

Registering an event listener through ActionScript

You can also respond to events by creating an event handler using ActionScript, and then registering an event listener for the event.

In this example, you use ActionScript to create an event handler for the creationComplete event of the Application container. You then register a listener for your event handler by using the addEventListener() method.

Tip: The creationComplete event for the Application form occurs at the start of your application, after the Application container and its children are initialized. The event handler for the creationComplete event provides a convenient place to run ActionScript code to register event listeners.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- HandlingEventsActionScript.mxml --> <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="200" height="200" creationComplete="creationCompleteHandler(event);"> <fx:Script> <![CDATA[ import flash.events.MouseEvent; import mx.events.FlexEvent; private function creationCompleteHandler(event:FlexEvent):void { // Listen for the click event on the Button control myButton.addEventListener (MouseEvent.CLICK, clickHandler); } private function clickHandler ( event:Event ):void { myLabel.text = "Hello, World!"; } ]]> </fx:Script> <s:Panel title="My Application" width="100%"> <s:layout> <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/> </s:layout> <s:Label id="myLabel" fontWeight="bold" fontSize="24"/> <s:Button id="myButton" label="Click Me!" /> </s:Panel> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Back to top

For more information

  • Using Events
  • Generating event handlers

Back to top


This work is licensed under a Creative Commons Attribution-Noncommercial 3.0 Unported License.

产品

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • 移动应用程序
  • Photoshop
  • Touch Apps

解决方案

  • 数字营销
  • 数字媒体
  • 网页体验管理

行业

  • 教育
  • 金融服务业
  • 政府部门

帮助

  • 产品帮助中心
  • 订货和退货
  • 下载和安装
  • 我的 Adobe

学习

  • Adobe 开发人员连接
  • Adobe TV
  • 培训和认证
  • 论坛
  • 设计中心

购买方式

  • 在线商店
  • 批量许可
  • 查找经销商

下载

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

公司

  • 新闻编辑室
  • 合作伙伴计划
  • 公司社会责任
  • 工作机会
  • 投资者关系
  • 事件
  • 法律
  • 安全
  • 联系 Adobe
选择您的地区 中国(更改)
选择您的地区 关闭

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

使用条款 | 隐私政策和 Cookies (更新)

京 ICP 备 10217899 号 京公网安备 110105010404