【问题标题】:Flex Menu Control - Click a button and a menu is displayed. How can I click that button a second time and hide that menu?Flex Menu Control - 单击按钮并显示菜单。如何再次单击该按钮并隐藏该菜单?
【发布时间】:2011-03-05 21:55:06
【问题描述】:

基本上,我有一个按钮,点击它会显示一个菜单。我想再次单击该菜单,然后菜单关闭。目前,每次单击按钮时,菜单都会重新打开。我在下面粘贴了 Flex livedoc 示例。如果单击该按钮,菜单会不断重新打开。

现在,我通过设置一个 var 来打开和关闭它,所以当点击按钮时它会进行检查。但是,如果您在屏幕之外单击,则会调度 HIDE 事件,并关闭菜单。这弄乱了正在设置的 open close var。

我怎样才能让下面的这个 Flex 示例在单击按钮时显示菜单,然后在第二次单击按钮时关闭菜单?考虑到如果您单击离开菜单,它会关闭它。

另外,我尝试了按钮的 MOUSE_DOWN_OUTSIDE 事件并设置了 preventDefault,并将 FlexMouseEvent event.cancelable 设置为 false。

更改为 PopUpMenuButton 不是一种选择。我需要大量剥皮。

这是 Flex 示例:

<mx:Script>
    <![CDATA[
        // Import the Menu control.
        import mx.controls.Menu;

        // Create and display the Menu control.
        private function createAndShow():void {
            var myMenu:Menu = Menu.createMenu(null, myMenuData, false);
            myMenu.labelField="@label";
            myMenu.show(10, 10);
        }
    ]]>
</mx:Script>

<!-- Define the menu data. -->
<mx:XML format="e4x" id="myMenuData">
    <root>
        <menuitem label="MenuItem A" >
            <menuitem label="SubMenuItem A-1" enabled="false"/>
            <menuitem label="SubMenuItem A-2"/>
        </menuitem>
        <menuitem label="MenuItem B" type="check" toggled="true"/>
        <menuitem label="MenuItem C" type="check" toggled="false"/>
        <menuitem type="separator"/>     
        <menuitem label="MenuItem D" >
            <menuitem label="SubMenuItem D-1" type="radio" 
                groupName="one"/>
            <menuitem label="SubMenuItem D-2" type="radio" 
                groupName="one" toggled="true"/>
            <menuitem label="SubMenuItem D-3" type="radio" 
                groupName="one"/>
        </menuitem>
    </root>
</mx:XML>

<mx:VBox>
    <!-- Define a Button control to open the menu -->
    <mx:Button id="myButton" 
        label="Open Menu" 
        click="createAndShow();"/>
</mx:VBox>

【问题讨论】:

    标签: apache-flex menu controls


    【解决方案1】:
    //Declare menu as an instance variable instead of a local var
    private var myMenu:Menu;
    
    //var to store menu status
    private var isMenuVisible:Boolean
    
    //Create the Menu control. call this from the creationComplete of the 
    //application or the Component that it is part of.
    private function createMenu():void 
    {
        var myMenu:Menu = Menu.createMenu(null, myMenuData, false);
        myMenu.labelField="@label";
        //menu fires an event when it is hidden; listen to it.
        myMenu.addEventListener(MenuEvent.MENU_HIDE, onMenuHidden);
    }
    private function onMenuHidden(e:MenuEvent):void
    {
        /*
        menuHide event fired whenever the menu or one of its submenus
        are hidden - makes sure it was indeed the main menu that was hidden
        I don't have compiler handy to test this, so if for 
        some reason comparing myMenu with e.menu doesn't work,
        try if(e.target == myMenu) instead; 
        And please let me know which one works via comment :)
        */
    
        if(e.menu == myMenu)
           isMenuVisible = false;
    }
    //call this from button's click 
    private function toggleMenu():void
    {
        if(isMenuVisible)
            myMenu.hide();
        else
            myMenu.show();
    }
    

    【讨论】:

    • 我刚刚运行了您的代码,但它并不是我想要的。单击该按钮时,将打开菜单。如果您再次单击该按钮,而菜单仍处于打开状态,它会关闭菜单并重新打开它。当您在菜单打开时单击按钮时,我希望菜单保持关闭状态。我正在寻找一种在菜单打开时单击按钮时阻止菜单关闭和重新打开的方法。我们还需要考虑到,当您点击离开菜单时,它也会通过调度 HIDE_MENU 事件来隐藏菜单。我需要一种方法来做到这一点。有什么想法吗?
    • 这就是它不起作用的原因:当您最初单击按钮时,toggleMenu 被调用并且 myMenu.show();叫做。 Menu 类在节目中添加了一个 MENU_HIDE 侦听器。当菜单打开时,您再次单击该按钮,或单击离开菜单,该 MENU_HIDE 将被调度。 Menu 类监听器发生在我们的 onMenuHidden() 被调用之前。所以当我们的 onMenuHidden() 被调用时,菜单已经被隐藏了,所以它会执行 myMenu.show()。有没有办法在 Menu 类监听器之前调用我们的 onMenuHidden() ?或者关于如何让它发挥作用的任何其他想法?
    • @Dana 点击按钮算作在菜单外点击。尝试使用addEventListener()(通过actionscript而不是mxml的click属性)添加toggleMenu作为点击事件监听器并设置更高的优先级(第4个参数)并从event.stopPropagation()event.stopImmediatePropagation()调用toggleMenu - 这应该可以防止按钮的点击事件被处理为点击外部。
    • Amarghosh,非常感谢您的帮助。这是我如何让它工作的:我在mouseDown(而不是点击)上调用MXML中的toggleMenu:mouseDown="toggleMenu(event)" 我使用callLater而不是event.stopImmediatePropagation。我害怕使用 stopImmediatePropagation,因为我的应用程序相当大,而且我不想造成任何奇怪的副作用。这是我在 mouseDown 时所做的: public function toggleMenu(e:Event):void { if (myMenu.visible) { myMenu.hide(); } else { this.callLater(function():void { myMenu.show(); }); } }
    猜你喜欢
    • 2010-11-18
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    相关资源
    最近更新 更多