Android如何实现中英文语言切换

发布时间:2021-12-30 16:07:54 作者:小新
来源:亿速云 阅读:606

这篇文章主要介绍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();
    }
 
}

代码到这里也就结束了,下面是添加国际化语言的简单步骤:

Android如何实现中英文语言切换

Android如何实现中英文语言切换 

Android如何实现中英文语言切换 

Android如何实现中英文语言切换

 Android如何实现中英文语言切换

 切记修改语言之后一定要重新加载页面,不然不会立即生效

SPUtil.put(SettingActivity.this,"isEN",isChecked);
recreate();

以上是“Android如何实现中英文语言切换”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Android应用语言切换功能
  2. Android应用实现多语言切换

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:Python中常用的内置函数有哪些

下一篇:CRM和C4C里的组织架构是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》