[英]Java using enum with switch statement
我查看了與此問題類似的 SO 上的各種問答,但還沒有找到解決方案。
我擁有的是一個枚舉,它代表了查看電視指南的不同方式......
在 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 來完成。
編輯對不起,我沒有完全擴展這個問題,因為我認為它是一個通用的 Java 問題。 我已經添加到問題中以進一步解釋。
沒有任何特定於 Android 的內容,這就是為什么我沒有將其標記為 Android 但枚舉是在Application
類中定義的,而我不想切換的代碼在Activity
。 枚舉是靜態的,因為我需要從多個活動訪問它。
您缺少的部分是從整數轉換為類型安全的枚舉。 Java 不會自動執行此操作。 有幾種方法可以解決這個問題:
guideView.GUIDE_VIEW_SEVEN_DAY.ordinal()
確定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
實現更有用/更不容易出錯,該實現將您的整數值作為參數來解析適當的枚舉值,並讓您集中檢查邊界。
如果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 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;
短關聯函數示例:
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”中的限定符是關鍵。
這應該按照您描述的方式工作。 你遇到了什么錯誤? 如果您可以粘貼您的代碼,那將有所幫助。
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
編輯:您確定要定義靜態枚舉嗎? 這對我來說聽起來不對。 枚舉很像任何其他對象。 如果您的代碼編譯並運行但給出了錯誤的結果,這可能就是原因。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.