更改本地化Android的语言时出错

时间:2013-02-20 18:12:51

标签: android button localization

此刻正在处理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());

    }
});

}

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

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

我做错了什么?任何建议都非常感谢。我希望从上面的代码中实现的是app以英语启动从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);

    }
});

仍然没有工作......

1 个答案:

答案 0 :(得分: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);
}
});

这只会将默认语言设置为西班牙语重新加载活动!