加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex中创建右键菜单

发布时间:2020-12-15 04:46:14 所属栏目:百科 来源:网络整理
导读:? Flex中创建右键菜单现实内容的类是ContextMenuItem类。ContextMenuItem 类表示上下文菜单中的项。每个ContextMenuItem 对象都有一个显示在上下文菜单中的标题(文本)。要向上下文菜单中添加新项,需要将其添加到 ContextMenu 对象的 customItems 数组。
? Flex中创建右键菜单现实内容的类是ContextMenuItem类。ContextMenuItem 类表示上下文菜单中的项。每个ContextMenuItem 对象都有一个显示在上下文菜单中的标题(文本)。要向上下文菜单中添加新项,需要将其添加到 ContextMenu 对象的 customItems 数组。 利用 ContextMenuItem 类的属性,您可以启用或禁用特定菜单项,也可以显示或隐藏菜单项。可以为 menuItemSelect 事件编写事件处理函数,以便在用户选择菜单项时为其添加功能。

--->点击阅读更多 ???

? ? ? ? 当然,定义右键菜单可以定制全局右键菜单,也可以定制特有的右键菜单,例如只有在DataGrid上才显示一个菜单,但是在其他地方则不显示。下面这段代码为本人写的一个小demo:

?

[html] view plain copy print ?
  1. <?xmlversionxmlversion="1.0"?encoding="utf-8"?>??
  2. <s:Application?xmlns:fx="http://ns.adobe.com/mxml/2009"??
  3. ??????????????xmlns:s="library://ns.adobe.com/flex/spark"??
  4. ??????????????xmlns:mx="library://ns.adobe.com/flex/mx"?minWidth="955"?minHeight="600"?creationComplete="app_creationCompleteHandler(event)">??
  5. ????<fx:Script>??
  6. ???????<![CDATA[?
  7. ???????????import?flash.utils.clearInterval;?
  8. ???????????import?flash.utils.setInterval;?
  9. ???????????
  10. ???????????import?mx.collections.ArrayCollection;?
  11. ???????????import?mx.collections.IList;?
  12. ???????????import?mx.controls.Alert;?
  13. ???????????import?mx.controls.dataGridClasses.DataGridItemRenderer;?
  14. ???????????import?mx.core.FlexGlobals;?
  15. ???????????import?mx.events.CloseEvent;?
  16. ???????????import?mx.events.FlexEvent;?
  17. ???????????import?mx.events.ListEvent;?
  18. ???????????[Bindable]private?var?contextMenuCustomItems:Array;?
  19. ???????????[Bindable]private?var?cm:ContextMenu;?
  20. ???????????[Bindable]private?var?songList:ArrayCollection=new?ArrayCollection([?
  21. ??????????????{showName:'恋上另一个人',songer:'游鸿明',filmName:'chirsyu_lslygr.flv'},?
  22. ??????????????{showName:'一天一万年',filmName:'chirisyu_ytywn.flv'},?
  23. ??????????????{showName:'下沙',filmName:'chirisyu_xs.flv'},?
  24. ??????????????{showName:'五月的雪',filmName:'chirisyu_wydx.flv'},?
  25. ??????????????{showName:'楼下那个女人',filmName:'chirisyu_lxngnr.flv'},?
  26. ??????????????{showName:'白色恋人',filmName:'chirisyu_bslr.flv'}?
  27. ???????????]);?
  28. ???????????protected?function?app_creationCompleteHandler(event:FlexEvent):void?
  29. ???????????{?
  30. ??????????????//?TODOAuto-generated?method?stub?
  31. ??????????????Alert.okLabel="确定";?
  32. ??????????????Alert.yesLabel="是";?
  33. ??????????????Alert.cancelLabel="否";?
  34. ??????????????var?aboutUserMenu:ContextMenuItem=new?ContextMenuItem("关于我们",true,true);?
  35. ???????????????aboutUserMenu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,aboutUserMenuHandler);?
  36. ??????????????contextMenuCustomItems=FlexGlobals.topLevelApplication.contextMenu.customItems;?
  37. ??????????????contextMenuCustomItems.push(aboutUserMenu);?
  38. ??????????????var?linkMenu:ContextMenuItem=new?ContextMenuItem("联系作者",true);?
  39. ??????????????linkMenu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,linkHandler);?
  40. ??????????????contextMenuCustomItems.push(linkMenu);?
  41. ???????????}?
  42. ???????????private?function?aboutUserMenuHandler(event:ContextMenuEvent):void{???
  43. ??????????????Alert.show("我的网盘?V1.0","提示");?
  44. ???????????}?
  45. ???????????protected?function?showList_clickHandler(event:MouseEvent):void?
  46. ???????????{?
  47. ??????????????var?obj:Object=showList.selectedItem;?
  48. ??????????????var?fileName:String=obj['showName']?
  49. ??????????????if(fileName!=""){?
  50. ??????????????????createContextMenu("删除歌曲",buyHandler);?
  51. ??????????????????showList.contextMenu=cm;?
  52. ??????????????}?
  53. ???????????}?
  54. ???????????private?function?linkHandler(event:ContextMenuEvent):void{?
  55. ??????????????Alert.show("QQ:996475895","提示");?
  56. ???????????}?
  57. ???????????
  58. ???????????private?function?createContextMenu(menuName:String,fun:Function):void?
  59. ???????????{?
  60. ??????????????cm?=?new?ContextMenu();?
  61. ??????????????cm.hideBuiltInItems();?
  62. ??????????????var?customItemsArr:Array?=?new?Array();?
  63. ??????????????var?customMenuItem:ContextMenuItem?=?newContextMenuItem(menuName,true);?
  64. ??????????????customMenuItem.addEventListener("menuItemSelect",?fun);?
  65. ??????????????customItemsArr.push(customMenuItem);?
  66. ??????????????cm.customItems?=?customItemsArr;?
  67. ???????????}?
  68. ???????????
  69. ???????????private?function?buyHandler(event:ContextMenuEvent):void?
  70. ???????????{?
  71. ??????????????var?showName:String=showList.selectedItem['showName'];?
  72. ??????????????Alert.show('确实要删除歌曲"'+showName+'"吗?',"提示",Alert.YES|Alert.CANCEL,this,yesOrCancleHandler,null,Alert.CANCEL);?
  73. ???????????}?
  74. ???????????private?function?yesOrCancleHandler(event:CloseEvent):void{?
  75. ??????????????if(event.detail==Alert.YES){?
  76. ??????????????????var?index:int=showList.selectedIndex;?
  77. ??????????????????songList.removeItemAt(index);?
  78. ??????????????????showList.dataProvider=songList;?
  79. ??????????????}?
  80. ???????????}?
  81. ???????]]>??
  82. ????</fx:Script>??
  83. ????<fx:Declarations>??
  84. ???????<!--?Place?non-visualelements?(e.g.,?services,?value?objects)?here?-->??
  85. ????</fx:Declarations>??
  86. ?????
  87. ???????<mx:DataGrid?id="showList"?x="228"?y="97"?width="563"?height="355"??
  88. ???????????????????click="showList_clickHandler(event)"dataProvider="{songList}"??fontSize="16"?fontWeight="bold">??
  89. ???????????<mx:columns>??
  90. ??????????????<mx:DataGridColumn?headerText="歌曲名"?dataField="showName"?width="250"/>??
  91. ??????????????<mx:DataGridColumn?headerText="演唱者"?dataField="songer"?/>??
  92. ??????????????<mx:DataGridColumn?headerText="链接地址"?dataField="filmName"?visible="false"?/>??
  93. ???????????</mx:columns>??
  94. ???????</mx:DataGrid>??
  95. ???
  96. </s:Application>??


?

? ? ? 此文件创建了一个全局右键菜单和一个局部右键菜单:当鼠标没有选择DataGird数据时显示的是“关于我们”,“联系作者”这两个菜单,当选中了DataGird的一个数据时则显示“删除歌曲”右键菜单。

? ? 当然,右键菜单的命名也是有规则的,如下所示:

  • 向上下文菜单添加的自定义菜单项不得超过 15 个。
  • 每个标题至少必须包含一个可见字符。
  • 控制字符、换行符和其他空白字符将被忽略。
  • 任何标题的长度都不能超过 100 个字符。
  • 与任何内置菜单项或其他自定义菜单项相同的标题将被忽略,不管匹配的菜单项是否可见。菜单标题在与内置标题或现有自定义标题比较时将不考虑大小写、标点符号或空白。
  • 不允许使用以下标题,但可以将这些词与其它词结合使用以形成自定义标题(例如,尽管不允许使用“粘贴”,但却允许使用“粘贴感觉良好”):


? ? ? ? ? ?Save

? ? ? ? ? ?Zoom In
? ? ? ? ? ?Zoom Out
? ? ? ? ? ?100%
? ? ? ? ? ? Show All
? ? ? ? ? ? Quality
? ? ? ? ? ? Play
? ? ? ? ? ? Loop
? ? ? ? ? ? Rewind
? ? ? ? ? ? Forward
? ? ? ? ? ? Back
? ? ? ? ? ? Movie not loaded
? ? ? ? ? ? About
? ? ? ? ? ? Print
? ? ? ? ? ? Show Redraw Regions
? ? ? ? ? ? Debugger
? ? ? ? ? ? Undo
? ? ? ? ? ? Cut
? ? ? ? ? ? Copy
? ? ? ? ? ? Paste
? ? ? ? ? ? Delete
? ? ? ? ? ? Select All
? ? ? ? ? ? Open
? ? ? ? ? ? Open in new window
? ? ? ? ? ? Copy link

  • 不管是本身还是与其它词结合使用,以下任何一个词都不能出现在自定义标题中:

? ? ? ? ? ? Adobe
? ? ? ? ? ?Macromedia
? ? ? ? ? ?Flash Player
? ? ? ? ? ?Settings


? ? ? ? ?注意:如果播放器运行在非英语系统上,标题字符串将同时与英语列表和对应的本地化字符串进行比较。例如我开始直接添加了“删除”。结果怎么都添加不上,原来“删除”与“Delete”是对应的。后来改成了“删除歌曲”才添加成功的!

??

? ? ? ? ?不过有一个小小的遗憾的,如果将此DataGrid添加到一个容器如Panel中时,右键菜单则失效,原因是Flex只有顶级容器才能创建右键菜单,后续我在看看这种情况下的右键菜单创建。

--->点击阅读更多 ???

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读