// ActionScript代码
import mx.controls.Menu; import flash.events.MouseEvent;
private var menu12:Menu; private function init():void { menu12 = new Menu(); menu12.labelField = "@label"; menu12.dataProvider = xmlDP; menu12.showRoot = false; menu12.width = popUpButton.width; popUpButton.popUp = menu12; }
// XML信息
<menu1 label="Some introduction" />
<menu2 label="Disabled State (disabled)." enabled="false" />
<sep1 type="separator" />
<menu3 label="parent">
<menu4 label="child1" />
</menu3>
<menu5 label="parent (disabled)" enabled="false">
<menu6 label="child1" />
<menu7 label="child2" />
<menu8 label="child3" />
</menu5>
<menu9 type="separator" />
<menu10 type="separator" />
<menu11 id="leftButton" label="Left" type="radio" groupName="radioGroup" toggled="true" enabled="true" />
<menu12 id="rightButton" label="Right" type="radio" groupName="radioGroup" enabled="false" />
<menu13 id="popupButton" label="Popup" type="radio" groupName="radioGroup" enabled="false" />
</root>
// component
<mx:PopUpButton id="popUpButton"
label="Please select an item"
openAlways="true"
creationComplete="init();" />
如何向popupButton中的菜单添加addevents。
答案 0 :(得分:1)
以下是我的表现....
为每个XML项添加一个属性,以存储每个菜单项的事件名称:
<menu4 label="child1" eventName="child1Event" />
在初始化父组件时为每个已知事件添加事件侦听器:
private function init():void
{
menu12 = new Menu();
...
addEventListener("child1Event", child1EventHandler);
}
private function child1EventHandler(event:Event):void
{
....
}
(您甚至可以解析XML以动态添加事件侦听器
private function init():void
{
menu12 = new Menu();
...
addEventListener("child1Event",
function(event:Event)
{
alert(event.type);
}
);
}
添加itemClick事件处理程序:
private function popUpButton_itemClickHandler(event:MenuEvent):void
{
if (event.item.@eventName != null)
dispatchEvent(new Event(event.item.@eventName));
}
<mx:PopUpButton id="popUpButton"
label="Please select an item"
openAlways="true"
creationComplete="init();"
itemClick="popUpButton_itemClickHandler(event);" />
答案 1 :(得分:0)
添加
menu12.addEventListener(MenuEvent.ITEM_CLICK,itemClickHandler);
在init函数的末尾然后添加监听器函数
private function itemClickHandler(event:MenuEvent):void{}
到你的剧本。
答案 2 :(得分:0)
我认为您不能按照自己的方式在特定的XML节点上放置事件处理程序。您必须查看进入的事件才能告诉应该运行哪个处理程序。看看Theo Hultberg的守卫概念,使过滤代码更清晰:
http://blog.iconara.net/2008/03/30/separating-event-handling-from-event-filtering/