无论如何,在这种情况下,您实际上只需要一个事件侦听器。该侦听器将侦听表单的任何更改,并且与更改内容相等的参数可用于事件侦听器函数。我将向您展示,但请记住,这是一种伪情况,通常我不会从方法调用这样简单的事情中分派事件,因为分派是隐含的,因此没有真正需要监听它。
首先是自定义事件
package com.yourDomain.events
{
import flash.events.Event;
public class FormEvent extends Event
{
//Public Properties
public static const CANCEL_FORM:int = "0";
public static const SHOW_FORM:int = "1";
public static const SEND_FORM:int = "2";
public static const STATE_CHANGED:String = "stateChanged";
//Private Properties
private var formState:int;
public function FormEvent(formState:int):void
{
super(STATE_CHANGED);
formState = formState;
}
}
}
因此,我们刚刚创建了自定义事件类,并且我们已经对其进行了设置,以便我们可以通过侦听器函数捕获状态,正如我将演示一旦完成将调度所述自定义事件的伪表单类。
请记住,这都是假设的,因为我不知道您的代码是什么样子或您如何实现的。重要的是要注意,当我调度事件时,我需要发送一个反映新状态的参数。
package com.yourDomain.ui
{
import flash.events.Event;
import flash.events.EventDispatcher;
import com.yourDomain.events.FormEvent;
public class Form extends EventDispatcher
{
public function Form():void
{
//Anything you want form to do upon instantiation goes here.
}
public function cancelForm():void
{
dispatchEvent(new Event(FormEvent.CANCEL_FORM);
}
public function showForm():void
{
dispatchEvent(new Event(FormEvent.SHOW_FORM);
}
public function sendForm():void
{
dispatchEvent(new Event(FormEvent.SEND_FORM);
}
}
}
最后,我们创建将监听它的文档类。请知道,我意识到创建一个在调用类的方法时触发的侦听器是不合逻辑的,因为您显然知道您调用了该方法,但对于这个示例,它会到期。
package com.yourDomain.ui
{
import com.yourDomain.ui.Form;
import com.yourDomain.events.FormEvent;
//Form is in the same package so we need not import it.
public class MainDocumentClass
{
private var _theForm:Form;
public function MainDocumentClass():void
{
_theForm = new Form();
_theForm.addEventListener(FormEvent.STATE_CHANGED, onFormStateChange, false, 0, true);
/*
The following three method calls each cause the
FormEvent.STATE_CHANGE event to be dispatched.
onFormStateChange is notified and checks what
the last change actually was.
*/
_theForm.cancelForm();
_theForm.showForm();
_theForm.sendForm();
}
private function onFormStateChange(e:FormEvent):void
{
switch(e.formState)
{
case CANCEL_FORM:
trace('The form was canceled');
break;
case SHOW_FORM:
trace('The form was revealed');
break;
case SEND_FORM:
trace('The form was sent');
break;
}
}
}
}
我希望这是有帮助的,它已经晚了,我可能需要稍后修改一些东西,但这应该有助于了解如何制作自己的事件并自定义事情的工作方式。