In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of Android supporting international multi-language, which is very detailed and has certain reference value. Friends who are interested must finish it!
Effect.
Train of thought
One: add related resource files and reference them.
Second: set the language type specified in configuration,configuration.
Three: just change the configuration when needed.
Realize
Create a new resource file for the relevant language type under the values folder of res
Right-click the new resource file, select Locale, and click the > > button.
Select Language and region (any region)
The final file name is the same as other language file names, strings is fine.
Add language-related processing to the onCreate and onConfigurationChanged methods in MyApplication (onConfigurationChanged is to deal with horizontal and vertical screen switching), and add configuration,configuration to the application context object to specify the current language.
Public class MyApplication extends Application {@ Overridepublic void onCreate () {super.onCreate (); languageWork ();} @ Overridepublic void onConfigurationChanged (Configuration newConfig) {super.onConfigurationChanged (newConfig); languageWork ();} private void languageWork () {/ / self-written toolkit (below) Locale locale = LanguageUtil.getLocale (this); LanguageUtil.updateLocale (this, locale);}}
LanguageUtil is as follows (unsorted partners can use it directly). In order to ensure that the language is switched from A to B, and then start the application and still use the B language, we need to store the B language locally. The next time you start the application, just set it up.
Public class LanguageUtil {/ * Chinese * / public static final Locale LOCALE_CHINESE = Locale.CHINESE;/** * English * / public static final Locale LOCALE_ENGLISH = Locale.ENGLISH;/** * Russian * / public static final Locale LOCALE_RUSSIAN = new Locale ("ru"); private static final String LOCALE_SP = "LOCALE_SP"; private static final String LOCALE_SP_KEY = "LOCALE_SP_KEY" Public static Locale getLocale (Context context) {SharedPreferences spLocale = context.getSharedPreferences (LOCALE_SP, Context.MODE_PRIVATE); String localeJson = spLocale.getString (LOCALE_SP_KEY, "); Gson gson = new Gson (); return gson.fromJson (localeJson, Locale.class);} pivate static void setLocale (Context pContext, Locale pUserLocale) {SharedPreferences spLocal = pContext.getSharedPreferences (LOCALE_SP, Context.MODE_PRIVATE); SharedPreferences.Editor edit = spLocal.edit (); String json = new Gson (). ToJson (pUserLocale) Edit.putString (LOCALE_SP_KEY, json); edit.apply ();} public static boolean updateLocale (Context context, Locale locale) {if (needUpdateLocale (context, locale)) {Configuration configuration = context.getResources (). GetConfiguration (); if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.JELLY_BEAN_MR1) {configuration.setLocale (locale);} else {configuration.locale = locale;} DisplayMetrics displayMetrics = context.getResources (). GetDisplayMetrics () Context.getResources () .updateConfiguration (configuration, displayMetrics); setLocale (context, locale); return true;} return false;} public static boolean needUpdateLocale (Context pContext, Locale newUserLocale) {return newUserLocale! = null & &! getCurrentLocale (pContext) .equals (newUserLocale);} public static Locale getCurrentLocale (Context context) {Locale locale If (Build.VERSION.SDK_INT > = Build.VERSION_CODES.N) {/ / 7.0 there are multiple language settings to get the top language locale = context.getResources (). GetConfiguration (). GetLocales (). Get (0);} else {locale = context.getResources (). GetConfiguration (). Locale;} return locale;}}
To switch, the main trigger action is activity's recreate () (the switch uses the new configuration). All activity is managed in activityManager. Just recreate () all the activity of the other (except the current language selection activity), and you can. LanguageUtil also determines whether it is necessary to switch languages. If the current language is consistent with the one you want to select, no further processing is required)
Public void onClick (View view) {boolean need = false; switch (view.getId ()) {case R.id.chinese: need = LanguageUtil.updateLocale (this, LanguageUtil.LOCALE_CHINESE); if (need) {/ / the common activity management tool ActivityManager.getInstance (). RecreateAllOtherActivity (this); Toast.makeText (this, "change to chinese", Toast.LENGTH_SHORT). Show () } else {Toast.makeText (this, "no need", Toast.LENGTH_SHORT). Show ();} break; case R.id.english: need = LanguageUtil.updateLocale (this, LanguageUtil.LOCALE_ENGLISH); if (need) {ActivityManager.getInstance (). RecreateAllOtherActivity (this); Toast.makeText (this, "change to english", Toast.LENGTH_SHORT). Show () } else {Toast.makeText (this, "no need", Toast.LENGTH_SHORT). Show ();} break; case R.id.russian: need = LanguageUtil.updateLocale (this, LanguageUtil.LOCALE_RUSSIAN); if (need) {ActivityManager.getInstance (). RecreateAllOtherActivity (this); Toast.makeText (this, "change to russian", Toast.LENGTH_SHORT). Show () } else {Toast.makeText (this, "no need", Toast.LENGTH_SHORT). Show ();} break;}
ActivityManager is as follows
Public class ActivityManager {private static final Stack sActivityStack = new Stack (); private static ActivityManager sActivityManager;private ActivityManager () {} public Stack getActivityStack () {return sActivityStack;} / * single instance * / public static ActivityManager getInstance () {if (sActivityManager = = null) {synchronized (ActivityManager.class) {sActivityManager = new ActivityManager ();}} return sActivityManager;} / * add Activity to stack * / public void addActivity (Activity activity) {sActivityStack.add (activity) } / * Delete Activity * / public void removeActivity (Activity activity) {if (sActivityStack.isEmpty ()) {return;} sActivityStack.remove (activity) in the stack;} / * get the current Activity (the last push in the stack) * / public Activity currentActivity () {Activity activity = sActivityStack.lastElement (); return activity } / * end the current Activity (the last push in the stack) * / public void finishActivity () {Activity activity = sActivityStack.lastElement (); finishActivity (activity);} / * end the specified Activity * / public void finishActivity (Activity activity) {if (activity! = null) {sActivityStack.remove (activity); activity.finish () }} / * end the Activity * / public void finishActivity (Class cls) {for (Activity activity: sActivityStack) {if (activity.getClass (). Equals (cls)) {finishActivity (activity); return;} / / get the Activitypublic Activity getActivity (Class cls) {for (Activity activity: sActivityStack) {if (activity.getClass (). Equals (cls)) {return activity of the specified class name }} return null;} / * end all Activity * / public void finishAllActivity () {for (int I = 0, size = sActivityStack.size (); I
< size; i++) { if (null != sActivityStack.get(i)) { sActivityStack.get(i).finish(); } } sActivityStack.clear();}public void finishAllOtherActivity(Activity activity) { for (int i = 0, size = sActivityStack.size(); i < size; i++) { if (null != sActivityStack.get(i) && sActivityStack.get(i) != activity) { sActivityStack.get(i).finish(); } } sActivityStack.clear();}public void recreateAllOtherActivity(Activity activity) { for (int i = 0, size = sActivityStack.size(); i < size; i++) { if (null != sActivityStack.get(i) && sActivityStack.get(i) != activity) { sActivityStack.get(i).recreate(); } }}/** * 退出应用程序 */public void AppExit() { try { finishAllActivity(); System.exit(0); } catch (Exception e) { } } } 将app的所有acitivity进行添加和移除,可以在BaseActivity里面进行。 @Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityManager.getInstance().addActivity(this);}@Overrideprotected void onDestroy() { super.onDestroy(); ActivityManager.getInstance().removeActivity(this);} 以上步骤,我们的语言切换算是完成了。但是,在API 26+以上版本,我们需要额外添加如下代码做兼容,没啥说的,SDK变动而已,跟着SDK走咯~(在activity或者BaseActivity添加) @Overrideprotected void attachBaseContext(Context newBase) { Context context = languageWork(newBase); super.attachBaseContext(context); }private Context languageWork(Context context) { // 8.0及以上使用createConfigurationContext设置configuration if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {return updateResources (context);} else {return context;}} @ RequiresApi (api = Build.VERSION_CODES.O) private Context updateResources (Context context) {Resources resources = context.getResources (); Locale locale= LanguageUtil.getLocale (context); if (locale==null) {return context;} Configuration configuration = resources.getConfiguration (); configuration.setLocale (locale); configuration.setLocales (new LocaleList (locale)); return context.createConfigurationContext (configuration) } these are all the contents of the article "sample Analysis of Android support for internationalized multilanguages". 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.