欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android國際化之中英文語言切換

 更新時間:2021年12月30日 14:36:00   作者:洋大闊天  
大家好,本篇文章主要講的是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方法即可。

里面有一個自定義的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,就是一個簡單的SharedPreferences內(nèi)容存取類:

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之后可以把引用數(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);
    }
 
    /**
     * 清空首選項
     *
     * */
    public static void clearData(Context context){
        SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        sp.edit().clear().apply();
    }
 
}

代碼到這里也就結(jié)束了,下面是添加國際化語言的簡單步驟:

?

?

?

?切記修改語言之后一定要重新加載頁面,不然不會立即生效

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

到此這篇關(guān)于Android國際化之中英文語言切換的文章就介紹到這了,更多相關(guān)Android中英文語言切換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android獲取點擊屏幕的位置坐標

    Android獲取點擊屏幕的位置坐標

    這篇文章主要為大家詳細介紹了Android獲取點擊屏幕的位置坐標,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android在ubuntu上過濾多條關(guān)鍵字日志

    Android在ubuntu上過濾多條關(guān)鍵字日志

    今天小編就為大家分享一篇關(guān)于Android在ubuntu上過濾多條關(guān)鍵字日志,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • Android Button按鈕點擊背景和文字變化操作

    Android Button按鈕點擊背景和文字變化操作

    這篇文章主要介紹了Android Button按鈕點擊背景和文字變化操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Android 7.0 SEAndroid app權(quán)限配置方法

    Android 7.0 SEAndroid app權(quán)限配置方法

    今天小編就為大家分享一篇Android 7.0 SEAndroid app權(quán)限配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Android通過ExifInterface判斷Camera圖片方向的方法

    Android通過ExifInterface判斷Camera圖片方向的方法

    今天小編就為大家分享一篇關(guān)于Android通過ExifInterface判斷相機圖片朝向的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Android  AIDL——進程通信機制詳解

    Android AIDL——進程通信機制詳解

    這篇文章主要介紹了Android AIDL——進程通信機制詳解的相關(guān)資料,并附簡單實例,和實現(xiàn)效果圖,需要的朋友可以參考下
    2016-10-10
  • Android?Activity啟動流程刨析

    Android?Activity啟動流程刨析

    Activity作為Android四大組件之一,他的啟動絕對沒有那么簡單。這里涉及到了系統(tǒng)服務(wù)進程,啟動過程細節(jié)很多,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值
    2022-08-08
  • Android進階手寫IPC通信框架告別繁瑣AIDL

    Android進階手寫IPC通信框架告別繁瑣AIDL

    這篇文章主要為大家介紹了Android進階手寫IPC通信框架告別繁瑣AIDL實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • asynctask的用法詳解

    asynctask的用法詳解

    Android UI操作并不是線程安全的并且這些操作必須在UI線程中執(zhí)行,本文將為您介紹asynctask的用法
    2012-11-11
  • Android  指紋識別開發(fā)實例

    Android 指紋識別開發(fā)實例

    這篇文章主要介紹了Android6.0 指紋識別開發(fā)實例的相關(guān)資料,需要的朋友可以參考下
    2016-09-09

最新評論