Android國(guó)際化之中英文語(yǔ)言切換
不想廢話,直接上干貨
@Override
protected void attachBaseContext(Context newBase) {
Locale newLocale;
if (SPUtil.getBoolean(newBase,"isEN")) {
//設(shè)置英文
newLocale = Locale.ENGLISH;
} else {
//設(shè)置中文
newLocale = Locale.SIMPLIFIED_CHINESE;
}
Context context = MyContextWrapper.wrap(newBase, newLocale);
super.attachBaseContext(context);
}
是的,直接在你繼承的BaseActivity里面重載(@Override)attachBaseContext方法即可。
里面有一個(gè)自定義的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,就是一個(gè)簡(jiǎn)單的SharedPreferences內(nèi)容存取類:
import android.content.Context;
import android.content.SharedPreferences;
public class SPUtil {
/**
* 萬(wàn)能的put方法 (能存儲(chǔ)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);
}
/**
* 清空首選項(xiàng)
*
* */
public static void clearData(Context context){
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
sp.edit().clear().apply();
}
}
代碼到這里也就結(jié)束了,下面是添加國(guó)際化語(yǔ)言的簡(jiǎn)單步驟:

?
?

?
?切記修改語(yǔ)言之后一定要重新加載頁(yè)面,不然不會(huì)立即生效
SPUtil.put(SettingActivity.this,"isEN",isChecked); recreate();
到此這篇關(guān)于Android國(guó)際化之中英文語(yǔ)言切換的文章就介紹到這了,更多相關(guān)Android中英文語(yǔ)言切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android獲取點(diǎn)擊屏幕的位置坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Android獲取點(diǎn)擊屏幕的位置坐標(biāo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android在ubuntu上過(guò)濾多條關(guān)鍵字日志
今天小編就為大家分享一篇關(guān)于Android在ubuntu上過(guò)濾多條關(guān)鍵字日志,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04
Android Button按鈕點(diǎn)擊背景和文字變化操作
這篇文章主要介紹了Android Button按鈕點(diǎn)擊背景和文字變化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Android 7.0 SEAndroid app權(quán)限配置方法
今天小編就為大家分享一篇Android 7.0 SEAndroid app權(quán)限配置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android通過(guò)ExifInterface判斷Camera圖片方向的方法
今天小編就為大家分享一篇關(guān)于Android通過(guò)ExifInterface判斷相機(jī)圖片朝向的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Android AIDL——進(jìn)程通信機(jī)制詳解
這篇文章主要介紹了Android AIDL——進(jìn)程通信機(jī)制詳解的相關(guān)資料,并附簡(jiǎn)單實(shí)例,和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-10-10
Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL
這篇文章主要為大家介紹了Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android 指紋識(shí)別開發(fā)實(shí)例
這篇文章主要介紹了Android6.0 指紋識(shí)別開發(fā)實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-09-09

