其實也不是多難的東西今天就是看到就想着寫下,由於看了好多博客寫的都是對不上號的東西,就自己寫下以後好查着看。
一、簡介
我們需要做的就是更新資源目錄的配置信息,同時我們要設置多個value比如value-en就是代表英語的value文件夾。
二、實現
首先我們來看下我們的Local
我就不全貼了以英語爲例
static public final Locale ENGLISH = createConstant("en", "");
裏面還有很多,我們就是要把設置這個Configuration,它 包含了設備的所有的配置信息,這些配置信息會影響應用獲取的資源。例如 string 資源,就是根據 Configuration 的 locale 屬性來判斷該取哪種語言的 string 資源,默認是 value 文件夾下的。我們就要把其中的local屬性切換了。
代碼如下
//獲取資源目錄
Resources resources = getResources();
//獲取資源目錄中的配置信息
Configuration config = resources.getConfiguration();
//獲取系統顯示區域信息
DisplayMetrics dm = resources.getDisplayMetrics();
//配置local信息
config.locale = newlocale;
//更新資源目錄的配置信息
resources.updateConfiguration(config, dm);
上面的newlocale
newlocale=Locale.ENGLISH
好了,我們要注意的是我們要重啓
Intent it = new Intent(context,MainActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);//這個必須添加
context.startActivity(it);
上面的代碼理論沒太大問題但是關鍵,我們設置的位置如果和要跳轉的activity不在一個棧中就不會有設置語言的效果,所以我們這裏要是殺死進程,保證重啓。
Intent it = new Intent(context,MainActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);//這個必須添加
context.startActivity(it);
// 殺掉進程
android.os.Process.killProcess(android.os.Process.myPid());
好了貼下全部代碼
package com.example.admin.yuyanqiehuan;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private Button btn_switch_en,btn_switch_zh;
private final String action = "cn.lyh.language";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_switch_en = (Button)findViewById(R.id.btn_switch_en);
btn_switch_en.setOnClickListener(clickListener);
btn_switch_zh = (Button)findViewById(R.id.btn_switch_zh);
btn_switch_zh.setOnClickListener(clickListener);
}
private View.OnClickListener clickListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(action);
switch(v.getId()){
case R.id.btn_switch_en:
switchLanguage(Locale.ENGLISH);
finish();
sendBroadcast(i);
break;
case R.id.btn_switch_zh:
switchLanguage(Locale.CHINESE);
finish();
sendBroadcast(i);
Intent it = new Intent(MainActivity.this,MainActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);//這個必須添加
startActivity(it);
break;
}
}
};
public void switchLanguage(Locale locale) {
//獲取資源目錄
Resources resources = getResources();
//獲取資源目錄中的配置信息
Configuration config = resources.getConfiguration();
//獲取系統顯示區域信息
DisplayMetrics dm = resources.getDisplayMetrics();
//配置local信息
config.locale = locale;
//更新資源目錄的配置信息
resources.updateConfiguration(config, dm);
}
}
package com.example.admin.yuyanqiehuan;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* 通過BroadcastReceiver重啓當前頁面,實現語言切換
*/
public class SwitchReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent it = new Intent(context,MainActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);//這個必須添加
context.startActivity(it);
// 殺掉進程
android.os.Process.killProcess(android.os.Process.myPid());
}
}
這裏你會發現你加上了這句話 就切換不了語言,因爲你殺死了全部的進程,程序重新開始加載默認的資源,所以你要用SharedPreferences吧語言存下來,在appliction中重新配置,這麼寫可以持久化改變語言的種類,因爲你殺死進程後,你的記錄就沒了下次還需要設置,這很顯然是不行的,而且這麼寫你就不用在其他的地方設置了就在application解決了。
package com.example.admin.yuyanqiehuan;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;
import java.util.Locale;
/**
* Created by admin on 2017/4/18.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//獲取資源目錄
Resources resources = getResources();
//獲取資源目錄中的配置信息
Configuration config = resources.getConfiguration();
//獲取系統顯示區域信息
DisplayMetrics dm = resources.getDisplayMetrics();
//配置local信息
config.locale = getSetLocale();
//更新資源目錄的配置信息
resources.updateConfiguration(config, dm);
}
// 得到設置的語言信息
private Locale getSetLocale() {
SharedPreferences pref = getApplicationContext().getSharedPreferences("date",MODE_PRIVATE);
int localNum = pref.getInt("local", 0);//第二個參數爲默認值
Log.e("當前語言種類",localNum+"====");
Locale locale=null;
switch (localNum){
case 0:
locale=Locale.CHINA;
break;
case 1:
locale=Locale.ENGLISH;
break;
}
return locale;
}
}
SharedPreferences pref = context.getSharedPreferences("date",MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
int localNum=intent.getIntExtra("local",0);
editor.putInt("local",localNum);
editor.commit();
Intent it = new Intent(context,MainActivity.class);
//
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);//這個必須添加
context.startActivity(it);
// 殺掉進程
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
最後提醒下,一個小問題你們可能遇不到,當你從應用中切出去,改變了系統語言的設置,當再切應用的時候,我發現語言也會變成系統語言。
這是因爲系統的會影響到我們的local,問題不大有需求就設置下判斷,這裏就不說了。。。。。()