1

目前在做一个安卓项目。我正在尝试实现一个按钮,该按钮允许我将语言从英语更改为西班牙语。这是我的代码:

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());

    }
});

}

该按钮不会更改语言。它离开它为英语。但是,如果我在活动顶部使用此代码运行我的项目,它会以西班牙语加载文本。

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

    }
});

还是行不通...

4

1 回答 1

1

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

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

This will just reload the activity with the default language set to Spanish!

于 2013-02-20T18:42:06.850 回答