溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android如何實(shí)現(xiàn)中英文語言切換

發(fā)布時(shí)間:2021-12-30 16:07:54 來源:億速云 閱讀:661 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹Android如何實(shí)現(xiàn)中英文語言切換,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

代碼

@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方法即可。

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

import android.content.Context;
import android.content.SharedPreferences;
 
public class SPUtil {
 
    /**
     * 萬能的put方法     (能存儲(chǔ)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);
    }
 
    /**
     * 清空首選項(xiàng)
     *
     * */
    public static void clearData(Context context){
        SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        sp.edit().clear().apply();
    }
 
}

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

Android如何實(shí)現(xiàn)中英文語言切換

Android如何實(shí)現(xiàn)中英文語言切換 

Android如何實(shí)現(xiàn)中英文語言切換 

Android如何實(shí)現(xiàn)中英文語言切換

 Android如何實(shí)現(xiàn)中英文語言切換

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

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

以上是“Android如何實(shí)現(xiàn)中英文語言切換”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI