如何在AIR应用程序中创建上下文菜单以及如何在Flex中的任何父项添加子菜单?
答案 0 :(得分:2)
不可能更简单,因为flex我不确定,但我认为它更容易。
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.events.ContextMenuEvent;
//create a new context menu to replace the default
var myContextMenu:ContextMenu = new ContextMenu();
//hide the default items
myContextMenu.hideBuiltInItems();
//create custom items:
var myMenuItem1:ContextMenuItem = new ContextMenuItem("Item 1");
var myMenuItem2:ContextMenuItem = new ContextMenuItem("Item 2");
var myMenuItem3:ContextMenuItem = new ContextMenuItem("Item 3");
//listen to the events of those items
myMenuItem1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,handleMenuItems);
myMenuItem2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,handleMenuItems);
myMenuItem3.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,handleMenuItems);
//put those items inside an array
var myItems:Array = [myMenuItem1,myMenuItem2,myMenuItem3];
//put the array containing the items inside the contextMenut
myContextMenu.customItems = myItems;
//now you can apply the context menu on any display object or the root mc
this.contextMenu = myContextMenu;
//now handle clicks
function handleMenuItems(event:ContextMenuEvent):void
{
switch (event.currentTarget)
{
case myMenuItem1:
trace("you clicked the first menu Item");
break;
case myMenuItem2:
trace("you clicked the second menu Item");
break;
case myMenuItem3:
trace("you clicked the third menu Item");
break;
}
}
答案 1 :(得分:1)