我有 3 个按钮来更改语言。所以我需要制定方法,在其中我可以将侦听器设置为按钮,每次按下它们时都会改变语言。我试过这个https://stackoverflow.com/a/2900144/1088229但这样它只改变了一次语言环境,似乎这不再起作用(如果我再次点击,它甚至没有响应)。所以我changeLanguageListener()
在监听器的末尾添加了,所以监听器被刷新了。
没关系,我是如何解决的还是有其他方法?
private void changeLanguageListener() {
final Button butEn = (Button) findViewById(R.id.button_language_en);
final Button butLt = (Button) findViewById(R.id.button_language_lt);
final Button butRu = (Button) findViewById(R.id.button_language_ru);
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
Button but = (Button) v;
Resources res = getResources();
String current = res.getConfiguration().locale.getCountry();
Log.i("Current", current);
String localeString = new String(current);
if (but.equals(butEn)) {
localeString = "en";
} else if (but.equals(butLt)) {
localeString = "lt";
} else if (but.equals(butRu)) {
localeString = "ru";
}
Log.i("Clicked", localeString);
if (!current.equalsIgnoreCase(localeString) && localeString.length() > 0) {
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(localeString.toLowerCase());
res.updateConfiguration(conf, dm);
//refresh menu
setGridView();
//added this line to get refreshed listener
changeLanguageListener();
}
}
};
butEn.setOnClickListener(listener);
butLt.setOnClickListener(listener);
butRu.setOnClickListener(listener);
}