Android國際化之中英文語言切換
不想廢話,直接上干貨
@Override
protected void attachBaseContext(Context newBase) {
Locale newLocale;
if (SPUtil.getBoolean(newBase,"isEN")) {
//設置英文
newLocale = Locale.ENGLISH;
} else {
//設置中文
newLocale = Locale.SIMPLIFIED_CHINESE;
}
Context context = MyContextWrapper.wrap(newBase, newLocale);
super.attachBaseContext(context);
}
是的,直接在你繼承的BaseActivity里面重載(@Override)attachBaseContext方法即可。
里面有一個自定義的MyContextWrapper:
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import java.util.Locale;
public class MyContextWrapper extends ContextWrapper {
public MyContextWrapper(Context base) {
super(base);
}
public static ContextWrapper wrap(Context context, Locale newLocale) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
}
return new ContextWrapper(context);
}
}
關(guān)于SPUtil,就是一個簡單的SharedPreferences內(nèi)容存取類:
import android.content.Context;
import android.content.SharedPreferences;
public class SPUtil {
/**
* 萬能的put方法 (能存儲String/int/boolean類型的值)
* @param context
* @param key
* @param value
*/
public static void put(Context context, String key, Object value) {
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
if (value instanceof String) {
edit.putString(key, (String) value);
} else if (value instanceof Integer) {
//JDK1.7之后可以把引用數(shù)據(jù)類型轉(zhuǎn)為基本數(shù)據(jù)類型
edit.putInt(key, (int) value);
} else if (value instanceof Boolean) {
edit.putBoolean(key, (boolean) value);
}
edit.apply();
}
/**
* 獲取String
* @param context
* @param key
* @return
*/
public static String getString(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
return sp.getString(key, "");
}
/**
* 獲取int
* @param context
* @param key
* @return
*/
public static int getInt(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
return sp.getInt(key, 0);
}
/**
* 獲取Boolean
* @param context
* @param key
* @return
*/
public static boolean getBoolean(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
return sp.getBoolean(key, false);
}
/**
* 清空首選項
*
* */
public static void clearData(Context context){
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
sp.edit().clear().apply();
}
}
代碼到這里也就結(jié)束了,下面是添加國際化語言的簡單步驟:

?
?

?
?切記修改語言之后一定要重新加載頁面,不然不會立即生效
SPUtil.put(SettingActivity.this,"isEN",isChecked); recreate();
到此這篇關(guān)于Android國際化之中英文語言切換的文章就介紹到這了,更多相關(guān)Android中英文語言切換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android在ubuntu上過濾多條關(guān)鍵字日志
今天小編就為大家分享一篇關(guān)于Android在ubuntu上過濾多條關(guān)鍵字日志,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04
Android 7.0 SEAndroid app權(quán)限配置方法
今天小編就為大家分享一篇Android 7.0 SEAndroid app權(quán)限配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Android通過ExifInterface判斷Camera圖片方向的方法
今天小編就為大家分享一篇關(guān)于Android通過ExifInterface判斷相機圖片朝向的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12

