gpt4 book ai didi

android - 只改变一次语言

转载 作者:行者123 更新时间:2023-11-29 21:34:44 26 4
gpt4 key购买 nike

在我的应用程序中,我尝试通过单击按钮(英语/意大利语)来更改语言。我选择默认语言环境英语。当打开 Activity 并按下意大利语按钮时,它发生了变化,但之后我无法选择英语。换句话说,语言只改变了一次。我做错了什么?

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;
}
}
}

最佳答案

我运行了您的代码并找到了解决方案。当您通过调用 setContentView 更新 View 时,您必须再次调用按钮 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;
}
}

关于android - 只改变一次语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18656633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com