網站首頁 編程語言 正文
不想廢話,直接上干貨
@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); } }
關于SPUtil,就是一個簡單的SharedPreferences內容存取類:
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之后可以把引用數據類型轉為基本數據類型 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(); } }
代碼到這里也就結束了,下面是添加國際化語言的簡單步驟:
?
?
?
?切記修改語言之后一定要重新加載頁面,不然不會立即生效
SPUtil.put(SettingActivity.this,"isEN",isChecked); recreate();
原文鏈接:https://blog.csdn.net/u012600997/article/details/122215721
相關推薦
- 2021-12-02 C/C++?Qt數據庫SqlRelationalTable關聯表詳解_C 語言
- 2022-09-26 折半插入排序算法詳解之C語言版
- 2023-01-14 GoLang逃逸分析講解_Golang
- 2022-06-01 Apache?Hudi靈活的Payload機制硬核解析_服務器其它
- 2022-04-23 R語言兩組變量特征相關關系熱圖繪制畫法_R語言
- 2022-10-12 go實現限流功能示例_Golang
- 2022-11-03 Python?頁面解析Beautiful?Soup庫的使用方法_python
- 2022-03-25 C++實現希爾排序算法實例_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支