如何添加欢迎 activity 以允许用户选择他的语言并更改所有 activity 的语言
how to add a welcome activity to allow user to choose his language and change language of all activitys
我正在尝试构建一个非常简单的应用程序,其中包含许多 activities
。
在这个应用程序中,我有一个 WelcomeActivity
供用户使用两个按钮选择他的 language
,一个用于 English
,一个用于 Arabic
然后我想要用户选择他的语言,该语言将在所有活动中发生变化,并且此 WelcomeActivity
将 运行 仅在第一次出现并且不会再次显示。
有什么帮助吗?
你可以这样做。
Locale locale = new Locale("ar”,"XX");
private void updateLocale(locale) {
final Configuration configuration = getResources().getConfiguration();
Locale.setDefault(baseLocal);
configuration.locale = baseLocal;
getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
}
如何在下次启动时恢复选择的语言。? [最好将其存储在共享首选项中。]我希望您可能知道如何存储。您只需要优先存储 "ar" 或 "en" 文本。在下次启动时,您必须回读并形成 Locale 并传递 updateLocale() 方法。
您可以使用 OnConfigurationChanged 方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Configuration configuration = getResources().getConfiguration();
Locale.setDefault(mCurrentLocale);
configuration.locale = mCurrentLocale;..locale from preference or latest selected.
getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
}
可以从您之前存储的共享首选项中读取 mCurrentLocale。
在这种情况下如何在共享首选项中存储语言
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(
this);
sharedPreferences().putString(KEY_STORE_CURRENT_LOCALE, language_code).commit();
在这种情况下如何从共享偏好中获取语言
final String current = sharedPreferences.getString(KEY_STORE_CURRENT_LOCALE,
language_code_english);
希望对您有所帮助!
您可以将语言值放在共享首选项中。就我而言,我将 en 和 np 作为英语和尼泊尔语。首先,我将值作为语言存储在共享偏好中,然后我有一个 lang 按钮。在我的例子中,我重新启动了我的应用程序,因为我的用户需要从另一个 api 再次加载数据。然后我将共享首选项字符串附加到 API.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
String language = preferences.getString("language","en");
lang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(language.equals("en")){
Util.setLanguage("np");
Intent restartIntent = new Intent(NavDrawerActivity.this,SplashActivity.class);
restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(restartIntent);
(NavDrawerActivity.this).finish();
}else {
Util.setLanguage("en");
Intent restartIntent = new Intent(NavDrawerActivity.this,SplashActivity.class);
restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(restartIntent);
(NavDrawerActivity.this).finish();
}
}
});
我正在尝试构建一个非常简单的应用程序,其中包含许多 activities
。
在这个应用程序中,我有一个 WelcomeActivity
供用户使用两个按钮选择他的 language
,一个用于 English
,一个用于 Arabic
然后我想要用户选择他的语言,该语言将在所有活动中发生变化,并且此 WelcomeActivity
将 运行 仅在第一次出现并且不会再次显示。
有什么帮助吗?
你可以这样做。
Locale locale = new Locale("ar”,"XX");
private void updateLocale(locale) {
final Configuration configuration = getResources().getConfiguration();
Locale.setDefault(baseLocal);
configuration.locale = baseLocal;
getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
}
如何在下次启动时恢复选择的语言。? [最好将其存储在共享首选项中。]我希望您可能知道如何存储。您只需要优先存储 "ar" 或 "en" 文本。在下次启动时,您必须回读并形成 Locale 并传递 updateLocale() 方法。
您可以使用 OnConfigurationChanged 方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Configuration configuration = getResources().getConfiguration();
Locale.setDefault(mCurrentLocale);
configuration.locale = mCurrentLocale;..locale from preference or latest selected.
getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
}
可以从您之前存储的共享首选项中读取 mCurrentLocale。
在这种情况下如何在共享首选项中存储语言
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(
this);
sharedPreferences().putString(KEY_STORE_CURRENT_LOCALE, language_code).commit();
在这种情况下如何从共享偏好中获取语言
final String current = sharedPreferences.getString(KEY_STORE_CURRENT_LOCALE,
language_code_english);
希望对您有所帮助!
您可以将语言值放在共享首选项中。就我而言,我将 en 和 np 作为英语和尼泊尔语。首先,我将值作为语言存储在共享偏好中,然后我有一个 lang 按钮。在我的例子中,我重新启动了我的应用程序,因为我的用户需要从另一个 api 再次加载数据。然后我将共享首选项字符串附加到 API.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
String language = preferences.getString("language","en");
lang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(language.equals("en")){
Util.setLanguage("np");
Intent restartIntent = new Intent(NavDrawerActivity.this,SplashActivity.class);
restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(restartIntent);
(NavDrawerActivity.this).finish();
}else {
Util.setLanguage("en");
Intent restartIntent = new Intent(NavDrawerActivity.this,SplashActivity.class);
restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(restartIntent);
(NavDrawerActivity.this).finish();
}
}
});