In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces Android how to achieve Chinese and English language switching, the article introduces in great detail, has a certain reference value, interested friends must read it!
Code
@ Override protected void attachBaseContext (Context newBase) {Locale newLocale; if (SPUtil.getBoolean (newBase, "isEN")) {/ / set English newLocale = Locale.ENGLISH;} else {/ / set Chinese newLocale = Locale.SIMPLIFIED_CHINESE;} Context context = MyContextWrapper.wrap (newBase, newLocale); super.attachBaseContext (context);}
Yes, just overload (@ Override) the attachBaseContext method directly in the BaseActivity you inherited.
There is a custom 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);}}
About SPUtil, it is a simple SharedPreferences content access class:
Import android.content.Context;import android.content.SharedPreferences; public class SPUtil {/ * omnipotent put method (can store values of type 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 can convert the reference data type to the basic data type edit.putInt (key, (int) value) } else if (value instanceof Boolean) {edit.putBoolean (key, (boolean) value);} edit.apply ();} / * get 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, ");} / * get 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) } / * get 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) } / * * clear preferences * / public static void clearData (Context context) {SharedPreferences sp = context.getSharedPreferences ("config", Context.MODE_PRIVATE); sp.edit (). Clear (). Apply ();}}
This is the end of the code, and here are the simple steps to add an internationalized language:
Remember to reload the page after changing the language, otherwise it will not take effect immediately
SPUtil.put (SettingActivity.this, "isEN", isChecked); recreate (); these are all the contents of the article "how to switch between Chinese and English in Android". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.