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

    }
});
}
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>キーが国の名前になり、値がロケールになる場所
を作成してから、

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 に答える