在Android中以编程方式更改应用程序中的语言
可能重复:在Android中以编程方式更改应用程序中的语言,android,android-activity,locale,Android,Android Activity,Locale,可能重复: 我想立即更改所有活动的设置活动.java中的语言。 并保存此选项(即使我退出应用程序或重新启动手机),而不考虑系统区域设置 我该怎么做 public class MyOnItemSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
我想立即更改所有活动的
设置活动.java
中的语言。并保存此选项(即使我退出应用程序或重新启动手机),而不考虑系统区域设置
我该怎么做
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
switch (pos) {
case 0:
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext()
.getResources().getDisplayMetrics());
break;
case 1:
Locale locale2 = new Locale("de");
Locale.setDefault(locale2);
Configuration config2 = new Configuration();
config2.locale = locale2;
getBaseContext().getResources().updateConfiguration(config2, getBaseContext()
.getResources().getDisplayMetrics());
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
公共类MyOnItemSelectedListener实现OnItemSelectedListener{
已选择公共无效项(AdapterView父项,
视图,int pos,长id){
开关(pos){
案例0:
语言环境=新语言环境(“en”);
setDefault(Locale);
配置配置=新配置();
config.locale=locale;
getBaseContext().getResources().updateConfiguration(配置,getBaseContext())
.getResources().getDisplayMetrics());
打破
案例1:
语言环境locale2=新语言环境(“de”);
setDefault(locale2);
Configuration config2=新配置();
config2.locale=locale2;
getBaseContext().getResources().updateConfiguration(配置2,getBaseContext())
.getResources().getDisplayMetrics());
打破
}
}
未选择公共无效(AdapterView父级){
//什么也不做。
}
}
1)在退出应用程序和重新启动之间,您可以保存如下选择:
创建应用程序类
public class MutawaApplication extends Application {
(http://developer.android.com/reference/android/app/Application.html)
在AndroidManifest.xml中注册它
android:name=".MyApplication"
在此类中,使用onCreate从SharedReferences读取语言。在应用程序中更改语言时将其保存到SharedReferences
2) 在您的活动中,在恢复时检查区域设置,如果需要,请进行
replaceContentView()
您的问题是什么?语言未更改?不立即应用的更改默认情况下不支持Android中语言的运行时间切换,因此每次配置更改时,您都必须手动重置区域设置。cfr