原文地址:
http://snowpard-android.blogspot.com/2013/03/programmatically-change-language-in.html
不过
Locale.setDefault(myLocale);
这句可以去掉。
而且用
android.content.res.Configuration config = getResources().getConfiguration();
就可以了,不需要新建对象。
更改应用语言后,通常需要刷新当前页面,即执行
Intent refresh = new Intent(this, this.getClass());
startActivity(refresh);
finish();
但这样会引起页面跳动,所以在原文中用的是setTexts(),全部重新赋值一遍的方法。
如果需要更改语言后,按返回键在之前的页面也同样用新的语言设置,需要重写activity的onResume方法。
续:
测试时发现当APP运行时,去改变的系统的语言设置,后导致APP重新加载资源,locale就又成了系统的locale。
解决步骤:
1.在配置文件 AndroidManifest 的Application节点下中加入 android:configChanges="locale|layoutDirection", 这样当系统/activity的配置改变时,不会重新生成activity,而是去调用 onConfigurationChanged 方法。
当去改系统的配置时,会调用 Application的onConfigurationChanged()方法,参照http://blog.csdn.net/tangcheng_ok/article/details/7483993,你应该就知道怎么做了。
当是activity的配置改变时,会调用activity的onConfigurationChanged(),重载一下这个方法就好了。
上面提到的 android:configChanges的属性(locale,layoutDirection"等)如何跟各项配置映射到,我还没去了解过。测试结果是语言改变时,的确只是调用了onConfigurationChanged()方法,但地理位置等配置改变时,却不调用onConfigurationChanged()方法,也不重新生成activity.就测试而言, 这个方案目前是work fine...
不能翻墙的同学凑合看一下我复制的原文吧:
Programmatically change language in Android-application
Preparing resources
Source code values-ru/strings.xml (Russian)
Source code values-fr/strings.xml (French)
Source code values-de/strings.xml (German)
Layout application will consist of four buttons that change the language of the application and the text box to display the greeting line:
Developing an application
- UI elements:
- Current local application:
- Changing the language in the application:
public
void
changeLang(String lang)
{
if
(lang.equalsIgnoreCase(
""
))
return
;
myLocale =
new
Locale(lang);
saveLocale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config =
new
android.content.res.Configuration();
config.locale = myLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
updateTexts();
}
|
- Save the current locale:
public
void
saveLocale(String lang)
{
String langPref =
"Language"
;
SharedPreferences prefs = getSharedPreferences(
"CommonPrefs"
, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(langPref, lang);
editor.commit();
}
|
- Loading a saved locale:
public
void
loadLocale()
{
String langPref =
"Language"
;
SharedPreferences prefs = getSharedPreferences(
"CommonPrefs"
, Activity.MODE_PRIVATE);
String language = prefs.getString(langPref,
""
);
changeLang(language);
}
|
- Updating the UI elements of the current screen (you need to update only the screen in which a change of locale):
private
void
updateTexts()
{
txt_hello.setText(R.string.hello_world);
btn_en.setText(R.string.btn_en);
btn_ru.setText(R.string.btn_ru);
btn_fr.setText(R.string.btn_fr);
btn_de.setText(R.string.btn_de);
}
|
public
void
onClick(View v) {
String lang =
"en"
;
switch
(v.getId()) {
case
R.id.btn_en:
lang =
"en"
;
break
;
case
R.id.btn_ru:
lang =
"ru"
;
break
;
case
R.id.btn_de:
lang =
"de"
;
break
;
case
R.id.btn_fr:
lang =
"fr"
;
break
;
default
:
break
;
}
changeLang(lang);
}
|
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
this
.txt_hello = (TextView)findViewById(R.id.txt_hello);
this
.btn_en = (Button)findViewById(R.id.btn_en);
this
.btn_ru = (Button)findViewById(R.id.btn_ru);
this
.btn_fr = (Button)findViewById(R.id.btn_fr);
this
.btn_de = (Button)findViewById(R.id.btn_de);
this
.btn_en.setOnClickListener(
this
);
this
.btn_ru.setOnClickListener(
this
);
this
.btn_fr.setOnClickListener(
this
);
this
.btn_de.setOnClickListener(
this
);
loadLocale();
}
|