2012-09-07 3 views
1

이 코드 중복을 줄이는 방법에 대해 고민하고 있습니다. 사용자가 언어를 선택할 수 있도록 TextToSpeech 엔진을 사용하고 로캘을 사용하고 있습니다.이 코드 중복을 줄이는 방법

language은 회 전자입니다. 별도의 기능에 TextToSpeech 객체의

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

추출 생성 : 그것은 익명 클래스 내에서 사용 할 수 있도록 인수 locfinal를 선언해야한다는

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); 
      } 
     } 
    }); 
} 

참고.

사용법 :

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

Lol 텔레파시. +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); 
       } 
      } 
      }); 
    } 
} 

}