1、自定义组件
2、自定义事件
3、结合使用
一、自定义组件(使用的是Flex组件,ActionScript组件相对麻烦点,可视化差点)
该组件由一个TextArea和两个Button组成,如图:
代码:
MyComponent.mxml
/**
* 1. 编译期间执行
* 2. 自定义组件一般可以通过dispatchEvent委派parent来处理某个事件,这就需要在parent中设置一个addEventLister来监听事件。
* 就像Button的Event.CLICK事件,除了可以通过addEventLister(Event.CLICK, clickHandle)来监听处理外,在Flex组件中还可以
* 直接 mx:Button click="clickHandle(event)" / 。怎么定义自定义组件也可以达到如此效果呢?
* 2.1) 内部声明:[Event(name="commit",type ="laxino.MyEvent")],type是指事件的类型(如1的类型是int,'aaa'的类型是String),name是指
* Event中的构造函数中type。
* 2.2) 外部使用:commit="commitHandle(myEvent)"是指为该组件注册了一个监听器,监听名字叫commit的事件,并通过commitHandle处理。事件的触发和
* 委派必须在组件内实现。
*
*/
/**
*
* Using the Event metadata tag
* You use the [Event] metadata tag to define events dispatched by a component so that the Flex compiler can recognize
* them as MXML tag attributes in an MXML file. You add the [Event] metadata tag in one of the following locations:
* ActionScript components Above the class definition, but within the package definition, so that the events are bound
* to the class and not a particular member of the class.
* MXML components In the mx:Metadata tag of an MXML file.
*
*/
[Event(name="commit",type ="laxino.MyEvent")]
[Event(name="preview",type ="laxino.MyEvent")]