Android如何实现中英文语言切换
发表于:2024-09-23 作者:千家信息网编辑
千家信息网最后更新 2024年09月23日,这篇文章主要介绍Android如何实现中英文语言切换,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!代码@Override protected void attachBas
千家信息网最后更新 2024年09月23日Android如何实现中英文语言切换
这篇文章主要介绍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); }}
关于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();
以上是"Android如何实现中英文语言切换"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
语言
内容
类型
中英
切换
代码
数据
方法
篇文章
价值
兴趣
国际
小伙
小伙伴
就是
是的
更多
步骤
知识
行业
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
质量技术监督数据库
巨杉数据库待遇怎样
学校网络安全占信息化比例
滨江学院 数据库期末试卷
巨人通力服务器怎么看故障
dota如何玩东南亚服务器
免费的加密dns服务器
嘉定区网络技术开发答疑解惑
商丘软件开发
机算机网络技术是干什么的
网络安全人人有责主题班会
中学生网络安全专题课视频
模拟山羊数据库在哪
小程序页面获取数据库数据
深圳福永软件开发公司
数据库置疑后 是否能删除
岗位培训软件开发机构
内蒙软件开发
web 服务器的管理员
edge访问svn服务器
软件开发电脑培训学校
no steam 服务器
西安京九道网络技术厂招聘
网络安全模式下联网
魔兽世界狮心服务器是哪个区
融媒体时代对网络技术要求
徐州众启软件开发
数据库的应用开发
达内软件开发java
对数数据库