在现今国际交流频繁的时代,因为做项目的关系,学习到了在APP内切换语言的方式。但是由于Android7.0在语言上有了改进,导致7.0以前与7.0及以后APP内切换语言的方式有了不同。
先看下效果图
下面是具体的实现:
Android 7.0及以后版本
AppLanguageUtils.java:
public class AppLanguageUtils {
public static Context attachBaseContext(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context);
} else {
return context;
}
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
if (MyApplication.languageFlag==0){
configuration.locale = Locale.ENGLISH;
}
else {
configuration.locale = Locale.CHINESE;
}
return context.createConfigurationContext(configuration);
}
}
然后在每个需要切换语言的Activity中添加如下代码:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(AppLanguageUtils.attachBaseContext(newBase));
}
Android7.0以前
MyApplication:
public class MyApplication extends Application {
public static final int LANGUAGE_ENGLISH=0;
public static final int LANGUAGE_CHINESE=1;
public static int languageFlag=LANGUAGE_CHINESE;
private static Context mContext = null;
@Override
public void onCreate() {
super.onCreate();
//首先mContext获得一下 Context全局的值
mContext = getApplicationContext();
//如果是第一次运行,获得系统的语言,并记录在MyApplication.languageFlag中
if(isFirstRun("Language")){
if(getLanguage().equals("zh-CN")){
languageFlag=LANGUAGE_CHINESE;
}
else{
languageFlag=LANGUAGE_ENGLISH;
}
}
//如果不是第一次运行就读取SharedPreferences中的数值
else{
SharedPreferences pref=getSharedPreferences("languageList",MODE_PRIVATE);
//读取SharedPreferences中的数值,没有时 默认1:中文
languageFlag=pref.getInt("language",1);
}
}
protected String getLanguage(){
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else locale = Locale.getDefault();
String language = locale.getLanguage() + "-" + locale.getCountry();
return language;
}
//Android6.0多语言切换
public static void changeAppLanguage(Resources resources, int languageFlag){
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
if (languageFlag==0){
configuration.locale = Locale.ENGLISH;
}
else {
configuration.locale = Locale.CHINESE;
}
resources.updateConfiguration(configuration,displayMetrics);
}
public static boolean isFirstRun(String firstRunType) {
//实例化SharedPreferences对象(第一步)
SharedPreferences sharedPreferences = getContext().getSharedPreferences(
"share", MODE_PRIVATE);
//实例化SharedPreferences.Editor对象(第二步)
boolean isFirstRun = sharedPreferences.getBoolean("isFirstRun"+firstRunType, true);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (!isFirstRun) {
return false;
} else {
//保存数据 (第三步)
editor.putBoolean("isFirstRun"+firstRunType, false);
//提交当前数据 (第四步)
editor.commit();
return true;
}
}
public static Context getContext(){
return mContext;
}
}
languageFlag记录不同的语言,为1的时候表示中文,为0的时候表示英文。
在MyApplication中除了切换语言,还可以在APP第一次运行时获取系统语言,如果不是第一次运行则读取上次用户选择的语言。
在改变语言的地方添加(例如点击改变语言按钮):
MyApplication.changeAppLanguage(getResources(), MyApplication.languageFlag);
以demo为例,这是点击切换语言按钮的代码:
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.change_language:
if(MyApplication.languageFlag==1) {
MyApplication.languageFlag = 0;
MyApplication.changeAppLanguage(getResources(), MyApplication.languageFlag);
SharedPreferences.Editor editor = getSharedPreferences("languageList", MODE_PRIVATE).edit();
editor.putInt("language", MyApplication.languageFlag);
editor.apply();
//伪切换语言
changeLanguage.setText("CHANGE LANGUAGE");
nextActivity.setText("NEXT ACTIVITY");
language.setText("This is MainActivity");
}
else{
MyApplication.languageFlag = 1;
MyApplication.changeAppLanguage(getResources(), MyApplication.languageFlag);
SharedPreferences.Editor editor = getSharedPreferences("languageList", MODE_PRIVATE).edit();
editor.putInt("language", MyApplication.languageFlag);
editor.apply();
//伪切换语言
changeLanguage.setText("切换语言");
nextActivity.setText("下一个活动");
language.setText("现在是主界面");
}
break;
default:
break;
}
}
这里有个“伪切换语言”,下面我会讲下
在点击切换语言后,UI界面不会立即刷新,语言还是之前的语言,需要重新进入Activity(或者更复杂的方法),语言才会改变。
在实际体验中,重新进入Activity对用户的体验造成极大影响,所以直接setText()改变按钮、TextView中的文字(同理也可以改变图片),这就是一种“伪切换语言”,虽然看上去改变了语言,但实际仅是调用了最简单的setText()。
在这里主要讲了一些在不同版本中App内切换语言的方法,demo会在之后上传。由于是第一次写博文,有什么不足欢迎批评指正,有什么问题也欢迎一起探讨。