我正在尝试从其显示名称设置我的文字转语音。问题是文本到语音需要setLanguage的Locale。我如何从显示语言进展到语言环境?我首先以这种方式设置我的回收清单:
public void getSystemLanguages() {
systemLanguages = Resources.getSystem().getAssets().getLocales();
for (int i = 1; i < systemLanguages.length; i++) {
String sL = systemLanguages[i];
Locale loc = Locale.forLanguageTag(sL);
String locDisplayResults = loc.getDisplayName();
languagesList.add(new Languages(locDisplayResults));
Collections.sort(languagesList, new Comparator<Languages>() {
@Override
public int compare(Languages languages1, Languages languages2) {
return languages1.getLocaleDisplay().compareTo(languages2.getLocaleDisplay());
}
});
recyclerAdapter.notifyDataSetChanged();
}
}
然后我有一个回收器适配器,如下所示:
public void onClick(View view) {
final Languages currentLanguages = languagesList.get(getAdapterPosition());
Snackbar snackbar;
snackbar = Snackbar.make(view, currentLanguages.getLocaleDisplay() + " selected", Snackbar.LENGTH_INDEFINITE)
.setAction("Next", new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MainActivity.class);
chosenLanguage = localeDisplay.getText().toString();
intent.putExtra("language_key", chosenLanguage);
v.getContext().startActivity(intent);
}
});
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(grey_blue);
snackbar.show();
}
我认为应该相当容易实现,尽管我花了一周多的时间没有成功。任何帮助都会很棒!
编辑:我正在添加我的语言课,以防任何人可以帮助我安德烈亚斯&#39;溶液:
public class Language {
public String localeDisplay;
public Language (String localeDisplay) {
this.localeDisplay = localeDisplay;
}
public String getLocaleDisplay() {
return localeDisplay;
} } 干杯!
以下是我尝试的内容:
public class Language {
public String localeDisplay;
public Locale mLocale;
public Language(String localeDisplay) {
this.localeDisplay = localeDisplay;
}
public Language(Locale mLocale){
this.mLocale = mLocale;
}
public String getLocaleDisplay() {
return localeDisplay;
}
public void setmLocale(Locale mLocale) {
this.mLocale = mLocale;
}
public Locale getmLocale() {
return mLocale;
}
}
新编辑:这是正确的版本:
public class Language implements Comparable <Language> {
public String localeDisplay;
public Locale mLocale;
public Language(Locale mLocale, String localeDisplay) {
if (mLocale != null)
this.mLocale = mLocale;
if (localeDisplay != null)
this.localeDisplay = localeDisplay;
}
public Locale getmLocale(){
return mLocale;
}
public String getLocaleDisplay(){
return localeDisplay;
}
public int compareTo(Language l){
return localeDisplay.compareTo(l.localeDisplay);
}
}
答案 0 :(得分:0)
您可以让广播接收器监听区域设置更改。 https://developer.android.com/reference/android/content/Intent.html#ACTION_LOCALE_CHANGED
这个广播接收器将填充一个用于填充此适配器的变量。
请注意,在新区域设置更改后,您需要notifyDatasetChanged()。
祝你好运,我希望它有所帮助:)