gpt4 book ai didi

java - 单击一个菜单即可更改所有 Activity 的语言

转载 作者:行者123 更新时间:2023-11-30 00:14:11 24 4
gpt4 key购买 nike

想要通过单选更改整个应用程序的语言。

MainActivity.java

 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static Button english,russian;
private static TextView chooseText;
private static Locale myLocale;

//Shared Preferences Variables
private static final String Locale_Preference = "Locale Preference";
private static final String Locale_KeyValue = "Saved Locale";
private static SharedPreferences sharedPreferences;
private static SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
setListeners();
loadLocale();
}

//Initiate all views
private void initViews() {
sharedPreferences = getSharedPreferences(Locale_Preference, Activity.MODE_PRIVATE);
editor = sharedPreferences.edit();

chooseText = (TextView) findViewById(R.id.choose_text);
english = (Button) findViewById(R.id.english);
russian = (Button) findViewById(R.id.russian);

}

//Set Click Listener
private void setListeners() {
english.setOnClickListener(this);
russian.setOnClickListener(this);

}


@Override
public void onClick(View view) {
String lang = "en";//Default Language
switch (view.getId()) {
case R.id.english:
lang = "en";
break;
case R.id.russian:
lang = "ru";
break;

}

changeLocale(lang);//Change Locale on selection basis
}


//Change Locale
public void changeLocale(String lang) {
if (lang.equalsIgnoreCase(""))
return;
myLocale = new Locale(lang);//Set Selected Locale
saveLocale(lang);//Save the selected locale
Locale.setDefault(myLocale);//set new locale as default
Configuration config = new Configuration();//get Configuration
config.locale = myLocale;//set config locale as selected locale
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());//Update the config
updateTexts();//Update texts according to locale
}

//Save locale method preferences
public void saveLocale(String lang) {
editor.putString(Locale_KeyValue, lang);
editor.commit();
}

//Get locale method in preferences
public void loadLocale() {
String language = sharedPreferences.getString(Locale_KeyValue, "");
changeLocale(language);
}

//Update text methods
private void updateTexts() {
chooseText.setText(R.string.tap_text);
english.setText(R.string.btn_en);
russian.setText(R.string.btn_ru);

}
}

我可以使用上面的代码更改单个 Activity 语言。但我想单击一次更改所有应用程序语言。请帮助我这样做。我正在尝试允许从设备设置访问语言

最佳答案

创建一个类 LocalHelper.java 来管理您的应用程序的本地化。

public class LocaleHelper {

private static final String SELECTED_LANGUAGE = "SELECTED_LANGUAGE";

public static Context onAttach(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
return setLocale(context, lang);
}

public static Context onAttach(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
return setLocale(context, lang);
}

public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}

public static Context setLocale(Context context, String language) {
persist(context, language);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}

return updateResourcesLegacy(context, language);
}

private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}

private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();

editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}

@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);

Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);

return context.createConfigurationContext(configuration);
}

@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);

Resources resources = context.getResources();

Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}

resources.updateConfiguration(configuration, resources.getDisplayMetrics());

return context;
}
}

并且在您的应用程序类中,您需要覆盖 attachBaseContext 并调用 LocaleHelper.onAttach() 来初始化应用程序中的语言环境设置。

public class MyApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
}
}

每当您的语言发生变化时,您需要调用

LocaleHelper.setLocale(this, yourNewLanguageCode);

这将更改所有其他 Activity ,但不会更改您更改语言的 Activity 。因此,您需要重新创建该 Activity 或更新该 Activity 的 View 。

要重新创建 Activity ,请调用

startActivity(new Intent(CurrentActivity.this, CurrentActivity.class))
finish();

如果您想更新 View 而不是重新创建 Activity ,请使用从 LocaleHelper.setLocale(this, yourNewLanguageCode)

返回的 context
Context context = LocaleHelper.setLocale(this, languageCode);
Resources resources = context.getResources();

textView.setText(resources.getString(R.string.textTitle));

注意:在 Android API 版本 24(Nougat) 之后,您需要覆盖所有 Activity 中的 attachBaseContext 以反射(reflect)更改。

关于java - 单击一个菜单即可更改所有 Activity 的语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47592675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com