Android Java:使用枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7006035/
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-10-30 18:14:00  来源:igfitidea点击:

Android Java: using enums

javaandroidenums

提问by Tianxiang Xiong

I'm wondering how to use enums in Java. I'm working on a recipe application for Android. I have a screen with four tabs in a TabHost and would like to refer to them through named constants, and I believe that it's best to do it through an enum.

我想知道如何在 Java 中使用枚举。我正在开发适用于 Android 的食谱应用程序。我在 TabHost 中有一个带有四个选项卡的屏幕,我想通过命名常量来引用它们,我相信最好通过枚举来完成。

private enum mTab {
    TAB_NAME(0), TAB_INGREDIENT(1), TAB_STEP(2), TAB_MEDIA(3);

    final int numTab;

    private mTab(int num){
        this.numTab = num;
    }

    public int getValue(){
        return this.numTab;
    }

};

Now I'd like to create a different menu for each tab. For example, for TAB_INGREDIENT I'd like a menu option called "Add ingredient", while for TAB_MEDIA I'd like a menu option called "Add image".

现在我想为每个标签创建一个不同的菜单。例如,对于 TAB_INGREDIENT,我想要一个名为“添加成分”的菜单选项,而对于 TAB_MEDIA,我想要一个名为“添加图像”的菜单选项。

I'm creating the menus through an onPrepareOptionsMenu(), like so

我正在通过 onPrepareOptionsMenu() 创建菜单,就像这样

public boolean onPrepareOptionsMenu(Menu menu) {
    // Clear menu before showing new menu
    menu.clear();

    super.onPrepareOptionsMenu(menu);

    // Create new menu based on current tab
    MenuInflater inflater = getMenuInflater();
    int tab = getTabHost().getCurrentTab();

    switch (tab) {
        ...
            }

    return true;
}

The problem is that I don't know what to put in the switch statement. More specifically, I don't know how to compare "tab", which is an integer corresponding to the currently selected tab, to an enum element.

问题是我不知道在 switch 语句中放什么。更具体地说,我不知道如何将“tab”(与当前选定的选项卡对应的整数)与枚举元素进行比较。

回答by Gopi

You can add a method as below in your mTab enum -

您可以在 mTab​​ 枚举中添加如下方法 -

public static mTab toMTab(int val) {
    mTab retMTab = null;
    for (mTab tempTab : mTab.values()) {
        if(tempTab.getValue() == val)  {
            retMTab = tempTab;
            break;
        }
    }
    return retMTab;
}

and then in your onPrepareOptionsMenu() you can do like this 0

然后在你的 onPrepareOptionsMenu() 你可以这样做 0

switch(mTab.toMTab(intVal)) {
  case TAB_NAME:
     ...
     break;
  case TAB_INGREDIENT:
     ..
     ...

}

回答by Alastair

Enum variables can be placed directly in switch statements in Java. I am however concerned that enum may not be the most appropriate option in this case because each of your tabs is doing something different: decorating or subclassing imho would be better. From what I've learnt from Josh Bloch, if you have to add methods to an enum, they should all be the same name, for example: operations on a calculator.

枚举变量可以直接放在 Java 中的 switch 语句中。但是,我担心 enum 在这种情况下可能不是最合适的选项,因为您的每个选项卡都在做不同的事情:装饰或子类化 imho 会更好。根据我从 Josh Bloch 那里学到的,如果您必须向枚举添加方法,它们都应该具有相同的名称,例如:计算器上的操作。

Each letter being part of the enum:

每个字母都是枚举的一部分:

Menu v;
...
    switch (v)
    {
        case A:
           ...
        case B:
        default:
    }