Android國際化之中英文語言切換
不想廢話,直接上干貨
@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è)簡單的SharedPreferences內(nèi)容存取類:
import android.content.Context;
import android.content.SharedPreferences;
public class SPUtil {
/**
* 萬能的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é)束了,下面是添加國際化語言的簡單步驟:

?
?

?
?切記修改語言之后一定要重新加載頁面,不然不會(huì)立即生效
SPUtil.put(SettingActivity.this,"isEN",isChecked); recreate();
到此這篇關(guān)于Android國際化之中英文語言切換的文章就介紹到這了,更多相關(guān)Android中英文語言切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android編程實(shí)現(xiàn)兩個(gè)Activity之間共享數(shù)據(jù)及互相訪問的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)兩個(gè)Activity之間共享數(shù)據(jù)及互相訪問的方法,簡單分析了Android中Activity數(shù)據(jù)共享與訪問的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android應(yīng)用接入微信分享的實(shí)例代碼
本篇文章主要介紹了Android應(yīng)用接入微信分享的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-07-07
Android開發(fā)實(shí)現(xiàn)從相冊中選擇照片功能詳解
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)從相冊中選擇照片功能,涉及Android權(quán)限控制、事件綁定、文件路徑與獲取等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Android進(jìn)程間大數(shù)據(jù)通信LocalSocket詳解
這篇文章主要為大家介紹了Android進(jìn)程間大數(shù)據(jù)通信LocalSocket詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android開發(fā)中WebView的簡單使用小結(jié)
WebView(網(wǎng)絡(luò)視圖)能加載顯示網(wǎng)頁,可以將其視為一個(gè)瀏覽器。它使用了WebKit渲染引擎加載顯示網(wǎng)頁。下面這篇文章給大家總結(jié)了Android中WebView的簡單使用,有需要的可以參考借鑒。2016-09-09
Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的代碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的實(shí)例代碼,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android中的應(yīng)用認(rèn)領(lǐng)總結(jié)
這篇文章主要介紹了Android中的應(yīng)用認(rèn)領(lǐng)總結(jié),本文講解了如何認(rèn)領(lǐng)、對(duì)未簽名包簽名、需要替換的簽名值、驗(yàn)證簽名等內(nèi)容,需要的朋友可以參考下2015-01-01

