gpt4 book ai didi

Android 无需重启即可在应用内切换语言

转载 作者:行者123 更新时间:2023-12-03 21:32:14 25 4
gpt4 key购买 nike

从 fragment (设置)中按 TextView调用DialogFragment , 在其中更改设置(语言)应用程序,我如何关闭 DialogFragment ,应用设置而不重新启动整个应用程序?

最好在 SingleChoice 上立即更改语言选择对话框。重新启动一切正常。这是代码DialogFragment :

public class LanguageDialogFragment extends DialogFragment {

final String[] itemsLang = {"English", "Russian", "Ukraine"};
final String[] items = {"en", "ru", "uk"};
String lang;
MainActivity activity = new MainActivity();

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Выберите свой родной язык")
.setSingleChoiceItems(itemsLang, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
lang = items[item];

}
})

.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
App.prefs.saveLanguage(lang);
App.changeLang(lang);

reload();

}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

}
});

return builder.create();
}

private void reload() {
Intent intent = getActivity().getIntent();
getActivity().finish();
Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(App.context.getPackageName());
startActivity(LaunchIntent);
}
}

以下是我想要实现的应用程序示例: https://play.google.com/store/apps/details?id=com.funeasylearn.english

它的截图:

enter image description here
enter image description here

最佳答案

试试下面的代码。您必须根据用户选择的语言创建新的语言环境,将其设置为当前语言环境并重新启动您的 Activity 。请注意,如果在您进行区域设置切换时其他 Activity 处于 Activity 状态,您可能必须重新启动它们。

public class LanguageDialogFragment extends DialogFragment {

private final String[] itemsLang = {"English", "Russian", "Ukraine"};
private final String[] items = {"en", "ru", "uk"};
private String lang;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Выберите свой родной язык")
.setSingleChoiceItems(itemsLang, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
lang = items[item];

}
}).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//When user submits, restart the activity in
//the new language
restartActivityInLanguage(lang);

}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Do nothing
}
});

return builder.create();
}

private void restartActivityInLanguage(String language) {
Locale locale = new Locale(language);
Configuration config = new Configuration();
config.locale = locale;
Resources resources = getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
getActivity().recreate();
}
}

关于Android 无需重启即可在应用内切换语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41364425/

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