简体   繁体   English

如何从菜单隐藏项目并显示另一个项目:Android

[英]How to hide item from menu and show another item :Android

I want to show save button when edit button clicked and hide edit button 我想在单击编辑按钮时显示保存按钮,并隐藏编辑按钮

And

show edit button when save button is clicked and hide save button. 单击保存按钮时显示编辑按钮,并隐藏保存按钮。

My menu file is as below: 我的菜单文件如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
    android:id="@+id/edit_button"
    android:icon="@drawable/edit_button"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="@string/edit"/> 

    <item
    android:id="@+id/save_button"
    android:icon="@drawable/save_button"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="@string/save"/> 

</menu>

Screenshot for reference : 截图供参考:

在此处输入图片说明

You must do this programmatically like here add: Dynamic control of action/menu items in ActionBar , delete: How do I hide a menu item in the actionbar? 您必须像在此那样以编程方式执行此操作 :添加: 在ActionBar中动态控制动作/菜单项 ,删除: 如何 在动作栏中 隐藏菜单项? , you could use a flag and then call invalidateOptionsMenu() to update the changes you made on the Menu items, i don't have an example right now but i think with this explanation you should achieve what you are trying to do. ,您可以使用一个标志,然后调用invalidateOptionsMenu()来更新您对菜单项所做的更改,我现在没有示例,但是我认为通过这种解释,您应该可以实现自己的目标。 Another way could be add both MenuItem in method onCreateOptionsMenu (or directly via UI XML) and then in method onPrepareOptionsMenu hide the edit button and show the save button with setVisible() method update with the method mentioned before, sorry for my english, still learning it. 另一种方法是在onCreateOptionsMenu方法中添加两个MenuItem(或直接通过UI XML),然后在onPrepareOptionsMenu方法中隐藏编辑按钮,并使用setVisible()方法显示保存按钮,并使用前面提到的方法进行更新,对不起,我的英语仍然在学习它。

我使用ViewSwitcher在两个不同的按钮之间切换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM