1

I'm stuck on how I can reduce this code duplication, I'm using the TextToSpeech engine and using locales so the user can select their language.

language is a 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

    }
});
}
4

5 回答 5

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);
} ...
于 2012-09-07T10:30:45.023 回答
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);
        }
    });
于 2012-09-07T10:33:48.097 回答
1

从我的头顶上,制作一个Map<String,Locale>where 键将是国家/地区的名称,值将是语言环境
,然后就可以了

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()));
        }
    }
});
于 2012-09-07T10:31:28.523 回答
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);
                }
            }
            });
    }
}

}

于 2012-09-07T10:51:27.243 回答
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;
   }
}

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

于 2012-09-07T10:33:32.963 回答