单击按钮切换语言 [英] Clicking a button to switch the language

查看:113
本文介绍了单击按钮切换语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击"sub_changelang"按钮时,它应将程序语言更改为法语.我得到以下代码来更改语言环境,但是我不知道如何刷新/更新应用程序以将语言更改为法语.

When I click on the "sub_changelang" button, it should change the program language to French for example. I got the following code to change the locale but I have no idea how to refresh/ pdate the app to change the language to French.

Button cl = (Button) findViewById(R.id.sub_changelang); 
cl.setOnClickListener(new OnClickListener()
{ 
    @Override 
    public void onClick(View v)
    { 
        Locale locale = new Locale("fr_FR"); 
        Locale.setDefault(locale); 
        Configuration config = new Configuration(); 
        config.locale = locale; 
    } 
});

它不起作用.我该如何解决?我尝试添加:

It doesn't work. How can I fix it? I tried to add:

MainActivity.this.getResources().updateConfiguration(config, MainActivity.this.getResources().getDisplayMetrics());

但是没有用.我也尝试过:

but it didn't work. I also tried:

getBaseContext().getResources().updateConfiguration(config,
                          getBaseContext().getResources().getDisplayMetrics());

它也不起作用.

android:configChanges="locale"

设置在AndroidMainfest.xml中的应用程序->活动"下

is set inside the AndroidMainfest.xml under application -> activity

推荐答案

我正在使用此代码设置语言环境

I am using this code to set locale

String languageToLoad  = "fr_FR";
     Locale locale = new Locale(languageToLoad); 
     Locale.setDefault(locale);
     Configuration config = new Configuration();
     config.locale = locale;
     context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());

Intent intent = new Intent(XYZ.this, XYZ.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

此处上下文是应用程序基础上下文. 也请尝试使用"fr"代替"fr_FR" ,因为我正在使用阿拉伯语区域设置,并且可以正常工作.

here context is application Base Context. Please also try "fr" instead of "fr_FR" because I am working for Arabic locale and its working fine.

您需要在更改语言环境后重新启动活动".

You need to restart your Activity after changing locale.

这篇关于单击按钮切换语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆