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

作者:jingxian 时间:2022-02-21 01:33:37 

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


package com.example.androidtest;

import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btnLang = (Button) findViewById(R.id.btn);
// 按下按钮改变语言类型,在“简体中文”和“英文”之间切换
btnLang.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(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);
//}
}
});
}

private void setLang(Locale l) {
// 获得res资源对象
Resources resources = getResources();
// 获得设置对象
Configuration config = resources.getConfiguration();
// 获得屏幕参数:主要是分辨率,像素等。
DisplayMetrics dm = resources.getDisplayMetrics();
// 语言
config.locale = l;
resources.updateConfiguration(config, dm);

// 刷新activity才能马上奏效
startActivity(new Intent().setClass(MainActivity.this,
 MainActivity.class));
MainActivity.this.finish();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

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

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

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

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

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

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

标签:android,当前语言,app
0
投稿

猜你喜欢

  • Android开发笔记之:深入理解多线程AsyncTask

    2023-07-22 15:16:06
  • 超全MyBatis动态代理详解(绝对干货)

    2023-11-14 02:28:19
  • Kotlin中ListView与RecyclerView的应用讲解

    2023-01-24 01:26:44
  • android studio使用SQLiteOpenHelper()建立数据库的方法

    2023-10-28 12:50:35
  • 利用Jackson解决Json序列化和反序列化问题

    2023-02-16 14:59:54
  • Java二维数组实现数字拼图效果

    2021-11-21 20:39:17
  • Android多媒体之VideoView视频播放器

    2023-11-12 14:38:35
  • Android形状图形与状态列表图形及九宫格图片超详细讲解

    2023-04-13 06:04:01
  • 关于@GetMapping和@GetMapping(value=““)的区别

    2023-11-27 11:07:37
  • 关于Java中的try-with-resources语句

    2022-10-21 16:49:54
  • Java数据导出功能之导出Excel文件实例

    2022-08-19 07:20:30
  • C#在运行时动态创建类型的实现方法

    2023-08-26 21:51:17
  • 关于maven打包时的报错: Return code is: 501 , ReasonPhrase:HTTPS Required

    2022-09-09 00:50:51
  • C#使用linq对数组进行筛选排序的方法

    2023-12-06 06:21:21
  • Android开发TextView内的文字实现自动换行

    2023-06-21 12:27:48
  • Java如何在沙箱环境中测试支付宝支付接口

    2023-11-02 14:55:15
  • 完美解决Android App启动页有白屏闪过的问题

    2021-11-18 02:12:31
  • Java集合ArrayList与LinkedList详解

    2022-11-11 12:14:31
  • c# 给pdf添加数字签名的步骤

    2022-04-05 17:58:01
  • SpringBoot对Druid配置SQL监控功能失效问题及解决方法

    2023-06-10 21:31:24
  • asp之家 软件编程 m.aspxhome.com