gpt4 book ai didi

android - 为本地化 Android 更改语言时出错

转载 作者:行者123 更新时间:2023-11-30 03:46:01 26 4
gpt4 key购买 nike

目前正在开发一个 Android 项目。我正在尝试实现一个按钮,允许我将语言从英语更改为西类牙语。这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//As soon as app is started up create an order object.
order = new Order();
order.setCovers(2);
order.setTable(1);
order.addToOrder(new MenuItem("Item 1", 12.99));
Toast.makeText(getApplicationContext(), "order size: " + order.getItems().size(), Toast.LENGTH_SHORT).show();


//reference to button and add listeners
orderBtn = (Button)findViewById(R.id.orderBtn);
orderBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Create a new intent and start up the sections activity...
i = new Intent(v.getContext(), SectionsActivity.class);
i.putExtra("data", order);
startActivity(i);
}
});



spanishBtn = (Button)findViewById(R.id.spanishBtn);
spanishBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Locale locale = new Locale("es");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

}
});

}

该按钮不会更改语言。它保留为英语。但是,如果我在 Activity 顶部使用此代码运行我的项目,它会加载西类牙语文本。

Locale locale = new Locale("es");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

我做错了什么?非常感谢任何建议。我希望通过上面的代码实现的是应用程序以英语启动,从 values-en 文件夹中获取字符串值。然后,当我单击该按钮时,它会从 values-es 文件夹中加载字符串值。

改变监听器:

    spanishBtn = (Button)findViewById(R.id.spanishBtn);
spanishBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Locale locale = new Locale("es");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
MainActivity.this.setContentView(R.layout.activity_main);

}
});

还是不行...

最佳答案

最好的办法是在设置默认语言环境后重新加载 Activity 。因此,将您的代码修改为:

spanishBtn = (Button)findViewById(R.id.spanishBtn);
spanishBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("es");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Intent i = new Intent(v.getContext(), YourActivityName.class);
startActivity(i);
}
});

这只会重新加载默认语言设置为西类牙语的 Activity !

关于android - 为本地化 Android 更改语言时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14987000/

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