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

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

问题描述

当我单击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 中的 application -> activity 下设置

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.

更改区域设置后,您需要重新启动 Activity.

You need to restart your Activity after changing locale.

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

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