选项多个活动共有的菜单项 [英] OptionsMenu items common for multiple activites

查看:86
本文介绍了选项多个活动共有的菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 设计指南表示应始终将帮助放在最后溢出菜单项(它永远不会出现在ActionBar中),并且应该存在于每个活动中,这样用户就不必查找它。对于设置,也建议采用类似的方法。

The Android design guide says that Help should always be placed as the last item of the overflow menu (it should never appear in the ActionBar) and also, that is should be present in every activity, so that users don't have to look for it. Similar approach is also recommended for Settings.

但是,我想知道什么是确保我应用中的所有活动都可以处理这些项目而不用过多处理的最佳方法?代码重复?将这些常见项目手动放入每个XML菜单文件中,然后在每个活动类中手动处理对它们的单击都是无稽之谈。

However, I'm wondering what is the best way to make sure that all the activities in my app handle these items without lots of code repetition? Putting these common items manually into every XML menu file and then manually handling clicks on each of them in every activity class is just nonsense.

由于我已经扩展了所有项目我从一个通用基类(提供一些便捷方法)进行活动,我想到了这种方法:在 BaseActivity 类中,我定义一个空的 initOptionsMenu ()方法,子类可以通过将其特定项添加到菜单中来覆盖(模板方法模式样式)。在 onCreateOptionsMenu()的开头调用此方法,然后基类在菜单末尾添加公共项(设置和帮助)。

Since I am already extending all of my activities from a common base class (which provides some convenience methods), I came up with this approach: In the BaseActivity class, I define an empty initOptionsMenu() method which the subclasses may override (template method pattern style) by adding their specific items to the menu. This method is called at the start of onCreateOptionsMenu() and then the base class adds the common items (settings and help) at the end of the menu.

onOptionsItemSelected()方法遵循标准模式-启用项目ID,在默认情况下,将处理传递给超类。再次,基类处理设置和帮助案例。

The onOptionsItemSelected() method follows the standard pattern - it switches on the item ID and in the default case, it passes the handing to the superclass. Again, the base class handles the setting and help cases.

public class BaseActivity extends Activity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        initOptionsMenu(menu);
        menu.add(Menu.NONE, R.id.menu_settings, Menu.NONE, R.string.menu_help);
        menu.add(Menu.NONE, R.id.menu_help, Menu.NONE, R.string.menu_help);
        return true;
    }

    protected void initOptionsMenu(Menu menu) {}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_settings:
                startActivity(new Intent(this, SettingsActivity.class));
                return true;
            case R.id.menu_help:
                startActivity(new Intent(this, HelpActivity.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

现在,每当我创建一个新活动,我扩展了 BaseActivity 。如果派生的活动为选项菜单提供了更多项目,则我不覆盖标准的 onOptionsItemSelected(),而是覆盖自定义的initOptionsMenu()方法并填充菜单那里。在 onOptionsItemSelected()中,我只需要处理此活动特定项目的情况,普通情况将由 super 调用。

Now, whenever I create a new activity, I extend the BaseActivity. If the derived activity provides some more items for the options menu, I do not override the standard onOptionsItemSelected(), but instead I override the custom initOptionsMenu() method and populate the menu there. In onOptionsItemSelected(), I need to handle only cases for the items specific for this activity, the common ones will be handled by the super call.

public class FooActivity extends BaseActivity {

    @Override
    protected void initOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.foo, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // cases for items in R.menu.foo

            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

是这种模式吗懂吗您能提出更好的方法吗?请分享您的想法。

推荐答案

我可能不使用 initOptionsMenu 方法。从具体实现中添加菜单后,我将仅调用 super.onCreateOptionsMenu()。我的 BaseActivity 将添加设置和帮助菜单,以便在 BaseActivity 中的

I might not use initOptionsMenu method. I will just call super.onCreateOptionsMenu() after adding my menu from concrete implementation. My BaseActivity will add settings and help menu so in BaseActivity:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, R.id.menu_settings, Menu.NONE, R.string.menu_help);
    menu.add(Menu.NONE, R.id.menu_help, Menu.NONE, R.string.menu_help);
    return true;
}

MainActivity 扩展 BaseActivity

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, R.id.menu_create_dummy, Menu.NONE, R.string.menu_dummy);
    menu.add(Menu.NONE, R.id.menu_delete_dummy, Menu.NONE, R.string.menu_dummy);
    return super.onCreateOptionMenu(menu);
}

这篇关于选项多个活动共有的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆