如何在同一活动中重新加载菜单android?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11194707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:17:18  来源:igfitidea点击:

how to reload menu android in the same activity?

androidandroid-menu

提问by Fabiano Palaoro

I have a menu in my app android. When I click on add favorites, I need to reload the menu options, making it appear del favorites in the options and not appear add favorites.

我的应用程序android中有一个菜单。当我单击添加收藏夹时,我需要重新加载菜单选项,使其在选项中显示为收藏夹,而不会出现添加收藏夹。

I don't want use reload activity because of the back button.

由于后退按钮,我不想使用重新加载活动。

My code:

我的代码:

public boolean onCreateOptionsMenu(Menu menu) {
    try
    {
        MenuItem menuInicio = menu.add(INICIO, INICIO, 0, "Início");
        menuInicio.setIcon(android.R.drawable.ic_menu_edit);

        MenuItem menuBusca = menu.add(BUSCA, BUSCA, 0, "Buscar");
        menuBusca.setIcon(android.R.drawable.ic_menu_search);

        SubMenu menuFavoritos = menu.addSubMenu(FAVORITOS, FAVORITOS, 0, "Favoritos");
        if(!phytoterapicContent.getPhytoterapicItem().getIsFav())
            menuFavoritos.add(FAVORITOS, ADD_FAV, 0, "Adicionar aos Favoritos");
        else
            menuFavoritos.add(FAVORITOS, DEL_FAV, 1, "Remover dos Favoritos");
        menuFavoritos.add(FAVORITOS, LIST_FAV, 2, "Listar Favoritos");
        menuFavoritos.setIcon(android.R.drawable.star_off);
        }
        catch (Exception e) {
        }            
        return super.onCreateOptionsMenu(menu);
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case INICIO:
            Intent it = new Intent(ShowPhytoterapicActivity.this, HomeActivity.class);
            startActivity(it);
            break;
        case BUSCA:
            Intent it3 = new Intent(ShowPhytoterapicActivity.this, ShowSearchParametersActivity.class);
            startActivity(it3);
            break;
        case ADD_FAV:
            try {
                Dao<PhytoterapicItem, Integer> phytoterapicItemDao = getHelper().getPhytoterapicItemDao();
                phytoterapicContent.getPhytoterapicItem().setIsFav(true);
                phytoterapicItemDao.update(phytoterapicContent.getPhytoterapicItem());
                Toast.makeText(ShowPhytoterapicActivity.this, "Adicionado aos Favoritos", Toast.LENGTH_LONG).show();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        case DEL_FAV:
            try {
                Dao<PhytoterapicItem, Integer> phytoterapicItemDao = getHelper().getPhytoterapicItemDao();
                phytoterapicContent.getPhytoterapicItem().setIsFav(false);
                phytoterapicItemDao.update(phytoterapicContent.getPhytoterapicItem());
                Toast.makeText(ShowPhytoterapicActivity.this, "Removido dos Favoritos", Toast.LENGTH_LONG).show();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        case LIST_FAV:
            Intent it5 = new Intent(ShowPhytoterapicActivity.this, ShowFavoritesActivity.class);
            startActivity(it5);
            break;
    }
    return true;
}

Thanks!

谢谢!

回答by Dheeresh Singh

use onPrepareOptionsMenu

使用onPrepareOptionsMenu

Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.

准备要显示的屏幕的标准选项菜单。这在菜单显示之前调用,每次显示。您可以使用此方法有效地启用/禁用项目或以其他方式动态修改内容。

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.clear();
    if(isChangedStat) {
        menu.add(0, MENUITEM, 0, "True");
    } else {
        menu.add(0, MENUITEM, 0, "False");
    }
    return super.onPrepareOptionsMenu(menu);
}

Please note two points

请注意两点

1- If possible just enable or disable menu item or looks possible in your case can change the title of same menu will work because menu.clear(); may need over attention while handling

1- 如果可能,只需启用或禁用菜单项或在您​​的情况下看起来可能可以更改同一菜单的标题,因为 menu.clear(); 处理时可能需要过度注意

2- as per link provided by Atlos

2- 根据 Atlos 提供的链接

On Android 2.3.x and lower, the system calls onPrepareOptionsMenu() each time the user opens the options menu (presses the Menu button).

在 Android 2.3.x 及更低版本上,每次用户打开选项菜单 ( presses the Menu button)时,系统都会调用 onPrepareOptionsMenu( )。

On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu()to request that the system call onPrepareOptionsMenu().

在 Android 3.0 及更高版本上,当菜单项出现在操作栏中时,选项菜单被视为始终处于打开状态。当事件发生并且您想要执行菜单更新时,you must call invalidateOptionsMenu()请求系统调用 onPrepareOptionsMenu()。

回答by trevor-e

Here is a relevant link: http://developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu

这是一个相关链接:http: //developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu

It depends what version of Android you are targeting. Not sure if you have read this part yet, but any other advice I have would be this verbatim.

这取决于您所针对的 Android 版本。不确定你是否已经阅读了这一部分,但我的任何其他建议都是逐字逐句的。