2012-09-07 172 views
1

我被困在如何減少此代碼重複,我使用TextToSpeech引擎和使用語言環境,以便用戶可以選擇他們的語言。如何減少此代碼重複

language是一個微調。

language.setOnItemSelectedListener(new OnItemSelectedListener() { 

    public void onItemSelected(AdapterView<?> parent, View arg1, 
     int pos, long id) { 
    System.out.println(parent.getItemAtPosition(pos).toString()); 
    if (parent.getItemAtPosition(pos).toString().equals("UK")) { 
     textToSpeech = new TextToSpeech(MainActivity.this, 
      new TextToSpeech.OnInitListener() { 

      @Override 
      public void onInit(int status) { 
       if (status != TextToSpeech.ERROR) { 
       textToSpeech.setLanguage(Locale.UK); 
       } 
      } 
      }); 
    } else if (parent.getItemAtPosition(pos).toString() 
     .equals("US")) { 
     textToSpeech = new TextToSpeech(MainActivity.this, 
      new TextToSpeech.OnInitListener() { 

      @Override 
      public void onInit(int status) { 
       if (status != TextToSpeech.ERROR) { 
       textToSpeech.setLanguage(Locale.US); 
       } 
      } 
      }); 

    } else if (parent.getItemAtPosition(pos).toString() 
     .equals("French")) { 
     textToSpeech = new TextToSpeech(MainActivity.this, 
      new TextToSpeech.OnInitListener() { 

      @Override 
      public void onInit(int status) { 
       if (status != TextToSpeech.ERROR) { 
       textToSpeech.setLanguage(Locale.FRANCE); 
       } 
      } 
      }); 

    } else if (parent.getItemAtPosition(pos).toString() 
     .equals("Italian")) { 
     textToSpeech = new TextToSpeech(MainActivity.this, 
      new TextToSpeech.OnInitListener() { 

      @Override 
      public void onInit(int status) { 
       if (status != TextToSpeech.ERROR) { 
       textToSpeech 
        .setLanguage(Locale.ITALIAN); 
       } 
      } 
      }); 

    } else if (parent.getItemAtPosition(pos).toString() 
     .equals("German")) { 
     textToSpeech = new TextToSpeech(MainActivity.this, 
      new TextToSpeech.OnInitListener() { 

      @Override 
      public void onInit(int status) { 
       if (status != TextToSpeech.ERROR) { 
       textToSpeech 
        .setLanguage(Locale.GERMAN); 
       } 
      } 
      }); 

    } 

    } 

    public void onNothingSelected(AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 

    } 
}); 
} 
+0

也許你應該嘗試[codereview.se(http://codereview.stackexchange.com) –

+0

我真的很感激我得到了答案,但爲什麼請求關閉這個問題?爲什麼不讓我知道我在這裏發佈了這個問題,所以我可以學習? – orange

+0

堆棧溢出是針對破損的代碼,Codereview是針對次優設計的工作代碼 –

回答

3

您可以創建一個地圖。

private static final Map<String, Locale> LOCALES = new LinkedHashMap<String, Locale>() {{ 
    put("US", Locale.US); 
    // many more 
} 


final Locale locale = LOCALES.get(parent.getItemAtPosition(pos).toString()); 
if(locale != null) 
    textToSpeech = new TextToSpeech(MainActivity.this, 
     new TextToSpeech.OnInitListener() { 
     @Override 
     public void onInit(int status) { 
      if (status != TextToSpeech.ERROR) 
       textToSpeech.setLanguage(locale); 
     } 
    }); 
5

提取創建文字轉語音對象到一個單獨的功能:

private TextToSpeech createTextToSpeech(final Locale loc) { 
    return new TextToSpeech(MainActivity.this, 
     new TextToSpeech.OnInitListener() { 

     @Override 
     public void onInit(int status) { 
      if (status != TextToSpeech.ERROR) { 
       setLanguage(loc); 
      } 
     } 
    }); 
} 

注意參數loc必須聲明final,以便它可以在匿名類內部使用。

用法:

... 
} else if (parent.getItemAtPosition(pos).toString().equals("French")) { 
    textToSpeech = createTextToSpeech(Locale.FRANCE); 
} ... 
+1

洛爾心靈感應。 +1 – Tudor

+2

仍然有很多重複。另外,也許我們應該在語言環境字符串和語言環境之間使用某種映射 –

1

從我的頭頂, 做出Map<String,Locale>,其中關鍵將是國家和值的名稱將是區域
然後就去做

textToSpeech = new TextToSpeech(MainActivity.this, 
    new TextToSpeech.OnInitListener() { 

    @Override 
    public void onInit(int status) { 
     if (status != TextToSpeech.ERROR) { 
      textToSpeech 
       .setLanguage(localeMap.get(parent.getItemAtPosition(pos).toString())); 
     } 
    } 
}); 
0

嘗試使用一個枚舉爲

public enum LANGLIST { 
    UK("uk", Locale.UK), 
    UK("swe", Locale.SWEIDHS); 

    public String lang; 
    public Locale loc; 
    private LANGLIST(String lang, Locale loc) { 
     this.lang = lang; 
     this.loc = loc; 
    } 
} 

然後循環遍歷枚舉中的所有元素。

1
public class TextToSpeechFactory { 

private static final Map<String, Locale> LOCALES = new HashMap<String, Locale>() {{ 
     put("US", Locale.US); 
     // many more 
    } 
}; 

public static TextToSpeech createInstance(String language){ 
    Locale l = LOCALES.get(language); 
    if(l == null) 
     throw new Exception("Languange "+ language + "is not valid!"); 
    else{ 
     return new TextToSpeech(MainActivity.this, 
      new TextToSpeech.OnInitListener() { 

      @Override 
      public void onInit(int status) { 
       if (status != TextToSpeech.ERROR) { 
       textToSpeech.setLanguage(l); 
       } 
      } 
      }); 
    } 
} 

}