android动态设置app当前运行语言的方法

作者:袖梨 2022-06-25

android开发中有时候碰到切换语言的需求,这时候需要通过代码动态改变当前运行语言。

 

 代码如下复制代码

packagecom.example.androidtest;

 

importjava.util.Locale;

 

importandroid.os.Bundle;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.content.res.Configuration;

importandroid.content.res.Resources;

importandroid.util.DisplayMetrics;

importandroid.view.Menu;

importandroid.view.View;

importandroid.widget.Button;

 

publicclassMainActivityextendsActivity {

 

    @Override

    protectedvoidonCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        Button btnLang = (Button) findViewById(R.id.btn);

        // 按下按钮改变语言类型,在“简体中文”和“英文”之间切换

        btnLang.setOnClickListener(newView.OnClickListener() {

             

            @Override

            publicvoidonClick(View v) {

                // 获取当前Locale(包含语言信息)

                Locale curLocale = getResources().getConfiguration().locale;

                 

                // 判断语言类型,有以下两种判断方式

                 

                // 方法一,通过Locale的equals方法

                // public boolean equals (Object object)

                //   Returns true if object is a locale with the same language, country and variant.

                if(curLocale.equals(Locale.SIMPLIFIED_CHINESE)) {

                    setLang(Locale.ENGLISH);

                }else{

                    setLang(Locale.SIMPLIFIED_CHINESE);

                }

                 

                // 方法二,通过语言码,getLanguage()方法可以获得对应语言码

                // public String getLanguage ()

                //  Returns the language code for this Locale or the empty string if no language was set.

//              if (curLocale.getLanguage().equals(Locale.SIMPLIFIED_CHINESE.getLanguage())) {

//                  setLang(Locale.ENGLISH);

//              } else {

//                  setLang(Locale.SIMPLIFIED_CHINESE);

//              }

            }

        });

    }

 

    privatevoidsetLang(Locale l) {

        // 获得res资源对象

        Resources resources = getResources();

        // 获得设置对象

        Configuration config = resources.getConfiguration();

        // 获得屏幕参数:主要是分辨率,像素等。

        DisplayMetrics dm = resources.getDisplayMetrics();

        // 语言

        config.locale = l;

        resources.updateConfiguration(config, dm);

         

        // 刷新activity才能马上奏效

        startActivity(newIntent().setClass(MainActivity.this,

            MainActivity.class));

        MainActivity.this.finish();

    }

 

    @Override

    publicbooleanonCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.activity_main, menu);

        returntrue;

    }

 

}

 

通过下面一行代码获得当前语言信息

Locale curLocale = getResources().getConfiguration().locale;

判断语言和设置语言部分有详细注释,就不做过多解释啦!

资源文件需要支持多语言环境,这样才能看到切换语言的效果!

 

创建values-en文件夹,并创建英文版的strings.xml文件。

相关文章

精彩推荐