我在我的Android应用中实现了一个设置屏幕,有一个ListPreference允许用户选择语言。选择列表中的项目,重新创建活动并自动翻译文字时,它可以正常工作,但是当按下取消按钮时,如果用户再次尝试更改语言,则效果不可见,但是当设置屏幕时关闭,文字已被完美翻译。
这是我的SettingsActivity
内部类建模我正在使用的片段:
public class SettingsActivity extends PreferenceActivity {
/** Preferences fields */
private static final String PREF_LANGUAGE = "pref_language";
/**
* Method called when the application is been started
*
* @param savedInstanceState the saved instance state
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
final String PREF_LANGUAGE = "pref_language";
super.onCreate(savedInstanceState);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String language = "en";
if (Locale.getDefault().getLanguage().equals("es")) {
language = "es";
}
String languagePref = settings.getString(PREF_LANGUAGE, language);
Locale locale = new Locale(languagePref);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
setContentView(R.layout.settings_activity);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.OnSharedPreferenceChangeListener prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
/**
* Method triggered when a shared preference changes
*
* @param sharedPreferences is the object which represents the shared preferences
* @param key is the key of the preference changed
*/
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
switch (key) {
case PREF_LANGUAGE:
Locale locale = new Locale(sharedPreferences.getString(key, ""));
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
recreate();
break;
}
}
};
prefs.registerOnSharedPreferenceChangeListener(prefListener);
}
/**
* Class which represents the fragment for the settings screen
*/
public static class GeograppPreferenceFragment extends PreferenceFragment {
/**
* Method triggered when the fragment is created
*
* @param savedInstanceState is the state of the fragment
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(settings);
SharedPreferences settings = getPreferenceManager().getSharedPreferences();
String language = "en";
if (Locale.getDefault().getLanguage().equals("es")) {
language = "es";
}
String languagePref = settings.getString(PREF_LANGUAGE, language);
getPreferenceManager().findPreference(PREF_LANGUAGE).setDefaultValue(languagePref);
}
}
}
首选项xml是:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/settings_display" />
<ListPreference
android:key="pref_language"
android:title="@string/settings_language_title"
android:summary="@string/settings_language_summary"
android:entries="@array/language_options"
android:entryValues="@array/language_values" />
</PreferenceScreen>
答案 0 :(得分:0)
很难理解你的问题,但我认为使用onResume()函数会帮助你解决问题。当您离开设置屏幕时,将调用onResume()函数,您可以在其中立即更新语言。
答案 1 :(得分:0)
最后,我可以通过使用指令setOnPreferenceChangeListener
定义针对该首选项的单个侦听器并删除OnSharedPreferenceChangeListener
方法中的OnCreate
来解决此问题