简体   繁体   English

更改列表首选项时刷新首选项片段

[英]Refresh preference fragment on changing list preference

I'm designing an application that supports 3 languages. 我正在设计一个支持3种语言的应用程序。 I have added an option to select the language in preference fragment. 我添加了一个选项来选择首选项片段中的语言。 The locale in the preference fragment will not change immediately after selecting the language from ListPreference . ListPreference选择语言后,首选项片段中的语言环境不会立即更改。
But if I navigate back to MainActivity and come back to SettingsActivity the locale will be changed in the preference screen as well. 但是,如果我导航回到MainActivity并返回到SettingsActivity则语言环境也会在首选项屏幕中更改。

I need a solution to change the locale in the PreferenceFragment as soon as there is a change in the ListPreference value (ie when the language is selected.) 我需要一种解决方案,以在ListPreference值发生更改时(即,选择语言时)立即更改PreferenceFragment中的语言环境。

Any help appreciated. 任何帮助表示赞赏。

UPDATE : Now I'm using custom list preferences class CustListPreference that extends ListPreference and implements DialogInterface.OnClickListener 更新 :现在我正在使用自定义列表首选项类CustListPreference ,它扩展了ListPreference并实现了DialogInterface.OnClickListener

I have defined an OK button in the list preference. 我在列表首选项中定义了“确定”按钮。 Now in the OnClick method when ok is pressed I'm updating the configuration using 现在在按OK的OnClick方法中,我正在使用更新配置

getContext().getResources().updateConfiguration(config, null); , but no luck. ,但没有运气。 The view is not getting updated. 该视图未更新。

Some one please suggest me how to refresh the activity or restart onClick of OK button. 有人请建议我如何刷新活动或重新启动OK按钮。

You need to restart you activity, after changing the locale. 更改语言环境后,您需要重新启动活动。

Or Try updating configuration, something like that: 或者尝试更新配置,如下所示:

public void changeLanguage(Configuration configuration) {
    configuration.locale = Locale.ENGLISH;//or other locale

    Locale.setDefault(configuration.locale);
    getBaseContext().getResources().updateConfiguration(configuration, null);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM