如何减少此代码重复

时间:2012-09-07 10:27:18

标签: java android optimization code-duplication

我一直坚持如何减少代码重复,我正在使用TextToSpeech引擎并使用区域设置,以便用户可以选择他们的语言。

language是一个Spinner。

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

    }
});
}

5 个答案:

答案 0 :(得分:5)

将TextToSpeech对象的创建提取到一个单独的函数中:

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 :(得分: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);
        }
    });

答案 2 :(得分:1)

从我的头顶, 创建一个Map<String,Locale>,其中key将是country的名称,value将是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()));
        }
    }
});

答案 3 :(得分: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);
                }
            }
            });
    }
}

}

答案 4 :(得分: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;
   }
}

然后遍历枚举中的所有元素。