在我的应用程序中,我通过单击按钮(英语/意大利语)尝试更改语言。我选择默认语言环境英语。当打开活动时,我按下意大利按钮它改变了但之后我不能选择英语。换句话说,语言只改变了一次。我做错了什么?
public class MainActivity extends DefaultActivity implements View.OnClickListener {
String LOCALE_ITALIAN = "it";
String LOCALE_ENGLISH = "en";
Locale mLocale;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocale = new Locale(LOCALE_ENGLISH);
Locale.setDefault(mLocale);
Configuration config = new Configuration();
config.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.main);
findButton(R.id.english).setOnClickListener(this);
findButton(R.id.italian).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.english:
mLocale = new Locale(LOCALE_ENGLISH);
Locale.setDefault(mLocale);
Configuration config = new Configuration();
config.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
MainActivity.this.setContentView(R.layout.main);
break;
case R.id.italian:
mLocale = new Locale(LOCALE_ITALIAN);
Locale.setDefault(mLocale);
Configuration config1 = new Configuration();
config1.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config1,
getBaseContext().getResources().getDisplayMetrics());
MainActivity.this.setContentView(R.layout.main);
break;
}
}
}
答案 0 :(得分:2)
我运行你的代码并找到解决方案。通过调用setContentView更新视图时,必须再次调用按钮onclick。我相信它会奏效。
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.english:
mLocale = new Locale(LOCALE_ENGLISH);
Locale.setDefault(mLocale);
Configuration config = new Configuration();
config.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
NonOrangeUserActivity.this.setContentView(R.layout.non_orange_user);
findButton(R.id.english).setOnClickListener(this);
findButton(R.id.italian).setOnClickListener(this);
break;
case R.id.italian:
mLocale = new Locale(LOCALE_ITALIAN);
Locale.setDefault(mLocale);
Configuration config1 = new Configuration();
config1.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config1,
getBaseContext().getResources().getDisplayMetrics());
NonOrangeUserActivity.this.setContentView(R.layout.non_orange_user);
findButton(R.id.english).setOnClickListener(this);
findButton(R.id.italian).setOnClickListener(this);
break;
}
}