欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

Android国际化之中英文语言切换,

来源: 开发者 投稿于  被查看 48605 次 评论:71

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国际化之中英文语言切换的文章就介绍到这了,更多相关Android中英文语言切换内容请搜索3672js教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持3672js教程!

您可能感兴趣的文章:
  • Android支持国际化多语言那点事(支持8.0+)
  • Android轻松实现多语言的方法示例
  • Android编程实现动态支持多语言的方法

用户评论