menuBar的根菜单响应

menuBar的itemClick事件仅对子菜单监听,如果要监听根菜单,可以添加change事件。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    
    
    <!-- 当根菜单无法响应itemClick时,可以监听menubar的chang事件来监听跟菜单-->
    
    <fx:Script>
        <![CDATA[
            import mx.events.MenuEvent;
            
            private function clickItem(event:MenuEvent):void
            {
                trace(event.label);
            }
            
            private function onChange(event:MenuEvent):void
            {
                if(event.label=="首页")
                    trace(event.label);
                else
                    return;
            }
            
        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:XMLListCollection id="navigationMenuData">  
            <fx:XMLList xmlns="">  
                <menuitem label="首页"/> 
                
                <menuitem label="计划管理">  
                    <submenu label="查看总招生计划" type="radio" groupName="managePlan"/>  
                    <submenu label="查看分专业招生计划" type="radio" groupName="managePlan"/>  
                    <submenu label="查看分省招生计划" type="radio" groupName="managePlan"/>  
                    <submenu type="separator"/>  
                    <submenu label="调整招生计划" type="radio" groupName="managePlan"/>  
                    <submenu label="查看调整记录" type="radio" groupName="managePlan"/>  
                    <submenu label="导入招生计划" type="radio" groupName="managePlan" />  
                </menuitem>  
                
                <menuitem label="录取管理">  
                    <submenu label="管理录取现场" type="radio" groupName="manageMatriculate"/>  
                    <submenu label="管理录取审核流程"  type="radio" groupName="manageMatriculate"/>  
                    <submenu label="查看录取审核记录"  type="radio" groupName="manageMatriculate"/>  
                </menuitem>         
            </fx:XMLList>  
        </s:XMLListCollection>  
        
    </fx:Declarations>
    
    <mx:MenuBar id="navigationMenuBar" 
                labelField="@label" 
                dataProvider="{navigationMenuData}"
                itemClick="clickItem(event)"
                change="onChange(event)"/>

</s:Application>

 但是change事件会响应两次,有时鼠标移动到菜单上也响应。

针对这种情况,可以使用click事件.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    
    
    <!-- 当根菜单无法响应itemClick时,可以监听menubar的chang事件来监听跟菜单-->
    
    <fx:Script>
        <![CDATA[
            import mx.controls.menuClasses.MenuBarItem;
            import mx.events.MenuEvent;
            
            private function clickItem(event:MenuEvent):void
            {
                trace(event.label);
            }
            
            private function onClick(event:MouseEvent):void
            {
                //用MenuBarItem.data是否为空来判断该MenuBarItem是否有子Menu
                if(menuBar.selectedIndex!=-1 && MenuBarItem(menuBar.getChildAt(menuBar.selectedIndex+1)).data=="")
                {
                    var label:String=MenuBarItem(menuBar.getChildAt(menuBar.selectedIndex+1)).data.@label;
                    trace(label);
                }
            }
            
        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:XMLListCollection id="menu">  
            <fx:XMLList xmlns="">  
                <menuitem label="首页"/> 
                
                <menuitem label="计划管理">  
                    <submenu label="查看总招生计划" type="radio" groupName="managePlan"/>  
                    <submenu label="查看分专业招生计划" type="radio" groupName="managePlan"/>  
                    <submenu label="查看分省招生计划" type="radio" groupName="managePlan"/>  
                    <submenu type="separator"/>  
                    <submenu label="调整招生计划" type="radio" groupName="managePlan"/>  
                    <submenu label="查看调整记录" type="radio" groupName="managePlan"/>  
                    <submenu label="导入招生计划" type="radio" groupName="managePlan" />  
                </menuitem>  
                
                <menuitem label="录取管理">  
                    <submenu label="管理录取现场" type="radio" groupName="manageMatriculate"/>  
                    <submenu label="管理录取审核流程"  type="radio" groupName="manageMatriculate"/>  
                    <submenu label="查看录取审核记录"  type="radio" groupName="manageMatriculate"/>  
                </menuitem>         
            </fx:XMLList>  
        </s:XMLListCollection>  
        
    </fx:Declarations>
    
    <mx:MenuBar id="menuBar" 
                labelField="@label" 
                dataProvider="{menu}"
                itemClick="clickItem(event)"
                click="onClick(event)"/>

</s:Application>

 

posted @ 2013-04-11 22:35  ywl01  阅读(250)  评论(0编辑  收藏  举报