Java使用带有switch语句的enum



我看了很多类似这个问题的问题,但还没有找到解决方案。

我所拥有的是一个枚举,它代表了观看电视指南的不同方式…

在NDroid Application类中

static enum guideView {
    GUIDE_VIEW_SEVEN_DAY,
    GUIDE_VIEW_NOW_SHOWING,
    GUIDE_VIEW_ALL_TIMESLOTS
}

…当用户更改视图时,事件处理程序从0-2接收int,我想做这样的事情…

Android Activity onClick(DialogInterface dialog, int which)事件处理程序

// 'which' is an int from 0-2
switch (which) {
    case NDroid.guideView.GUIDE_VIEW_SEVEN_DAY:
    ...
    break;
}

我已经习惯了c#的枚举和select/case语句,它们允许上面的事情,我知道Java做的事情不同,但我就是不知道我需要做什么。

我将不得不诉诸if语句吗?可能只有3种选择,所以我可以这样做,但我想知道如何在Java中使用switch-case来完成。

EDIT对不起,我没有完全扩展这个问题,因为我把它看作是一个通用的Java问题。我在这个问题上加了一点,以便进一步解释。

没有任何Android特定的东西,这就是为什么我没有将其标记为Android,但枚举是在Application类中定义的,我不想切换的代码是在Activity中。枚举是静态的,因为我需要从多个活动访问它。

您缺少的部分是从整数转换为类型安全enum。Java不会自动完成。有几种方法可以做到这一点:

  1. 使用静态final整型列表而不是类型安全的enum,并打开接收到的整型值(这是java 5之前的方法)
  2. 打开指定的id值(如heneryville所述)或枚举值的序数值;即guideView.GUIDE_VIEW_SEVEN_DAY.ordinal()
  3. 确定int值所表示的枚举值,然后打开枚举值

    enum GuideView {
        SEVEN_DAY,
        NOW_SHOWING,
        ALL_TIMESLOTS
    }
    // Working on the assumption that your int value is 
    // the ordinal value of the items in your enum
    public void onClick(DialogInterface dialog, int which) {
        // do your own bounds checking
        GuideView whichView = GuideView.values()[which];
        switch (whichView) {
            case SEVEN_DAY:
                ...
                break;
            case NOW_SHOWING:
                ...
                break;
        }
    }
    

    你可能会发现写一个自定义的valueOf实现更有帮助/更不容易出错,它把你的整数值作为参数来解析适当的enum值,让你集中你的边界检查。

如果whichView是GuideView Enum的对象,下面的工作正常。请注意,case之后没有限定符。

switch (whichView) {
    case SEVEN_DAY:
        ...
        break;
    case NOW_SHOWING:
        ...
        break;
}

枚举不应该像NDroid.guideView.GUIDE_VIEW_SEVEN_DAY那样在case标签中限定,相反,您应该删除限定并使用GUIDE_VIEW_SEVEN_DAY

我喜欢Java enum的一些用法:

  1. .name()允许获取String类型的enum名称。
  2. .ordinal()允许您获得整数值,以0为基础。
  3. 可以为每个枚举附加其他值参数。
  4. ,当然,开关启用。

带值参数的enum:

    enum StateEnum {
        UNDEFINED_POLL  ( 1 * 1000L,       4 * 1000L),
        SUPPORT_POLL    ( 1 * 1000L,       5 * 1000L),
        FAST_POLL       ( 2 * 1000L,  4 * 60 * 1000L),
        NO_POLL         ( 1 * 1000L,       6 * 1000L); 
        ...
    }

开关的例子:

private void queuePoll(StateEnum se) {
    // debug print se.name() if needed
    switch (se) {
        case UNDEFINED_POLL:
            ...
            break;
        case SUPPORT_POLL:
            ...
            break;

这应该以您描述的方式工作。你得到了什么错误?如果你能粘贴你的代码,那将会有帮助。

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

编辑:您确定要定义静态枚举吗?我觉得这听起来不对。enum与其他对象非常相似。如果您的代码编译和运行,但给出了不正确的结果,这可能就是原因。

短关联函数示例:

public String getIcon(TipoNotificacao tipo)
{
    switch (tipo){
        case Comentou : return "fa fa-comments";
        case ConviteEnviou : return "icon-envelope";
        case ConviteAceitou : return "fa fa-bolt";
        default: return "";
    }
}

就像@Dhanushka说的,省略"switch"是键里面的限定符

enumerations accessing is very simple in switch case
private TYPE currentView;
//declaration of enum 
public enum TYPE {
        FIRST, SECOND, THIRD
    };
//handling in switch case
switch (getCurrentView())
        {
        case FIRST:
            break;
        case SECOND:
            break;
        case THIRD:
            break;
        }
//getter and setter of the enum
public void setCurrentView(TYPE currentView) {
        this.currentView = currentView;
    }
    public TYPE getCurrentView() {
        return currentView;
    }
//usage of setting the enum 
setCurrentView(TYPE.FIRST);
avoid the accessing of TYPE.FIRST.ordinal() it is not recommended always

我就是这样做的

public enum State
{
    // Retrieving, // the MediaRetriever is retrieving music //
    Stopped, // media player is stopped and not prepared to play
    Preparing, // media player is preparing...
    Playing, // playback active (media player ready!). (but the media player
                // may actually be
                // paused in this state if we don't have audio focus. But we
                // stay in this state
                // so that we know we have to resume playback once we get
                // focus back)
    Paused; // playback paused (media player ready!)
    //public final static State[] vals = State.values();//copy the values(), calling values() clones the array
};
public State getState()
{
        return mState;   
}

并在Switch语句

中使用
switch (mService.getState())
{
case Stopped:
case Paused:
    playPause.setBackgroundResource(R.drawable.selplay);
    break;
case Preparing:
case Playing:
    playPause.setBackgroundResource(R.drawable.selpause);
    break;    
}

最新更新