我正在开发一个使用“SharedPreferences”允许用户从语言环境列表中选择语言环境的应用程序设置活动的 android 项目。
当我使用“ listPreferences.setvalue() ”根据当前区域设置设置默认选定值时,会创建两次 SettingsActivity!
以下是我的代码:
private void setLanguagePrefDefaultValues() {
// Current Locale
Locale sysLocale = getResources().getConfiguration().locale;
// Language Preference
ListPreference languagePref = (ListPreference) findPreference(getString(R.string.pref_language_key));
// Edit Preference Summary
languagePref.setSummary(sysLocale.getDisplayName(sysLocale));
// Set Language Preference to Current Locale
if (sysLocale.getCountry() != "") {
languagePref.setValue(sysLocale.getLanguage() + "_" + sysLocale.getCountry());
} else {
languagePref.setValue(sysLocale.getLanguage());
}
}
这个方法是从onCreate()方法调用的
我注意到,当调用“ setSummary(...) ”方法时没有问题,但是当调用“ setValue() ”时会出现问题
设置活动类:
import java.util.Locale;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import com.telephoenic.simgateway.R;
/**
* Settings Activity
*
* @author Abd Alrahman Shoman
* @since September 2013
* @version 1.0.0
*
*/
public class SettingsActivity extends PreferenceActivity implements
SharedPreferences.OnSharedPreferenceChangeListener {
public static boolean updatedLocale = false;
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_pref_file);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_title_bar);
TextView title = (TextView) findViewById(R.id.textView_titleBar_title);
title.setText(getResources()
.getString(R.string.title_activity_settings));
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
hideFirstLaunchPref();
setLanguagePrefDefaultValues();
setProfileEditPrefIntent();
}
@Override
protected void onStop() {
super.onStop();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(getString(R.string.pref_language_key))) {
Locale locale = null;
String languageCode = sharedPreferences.getString(getString(R.string.pref_language_key), "");
if (languageCode.length() > 2) {
char ch3 = languageCode.charAt(2);
if (ch3 == '_') {
String language = languageCode.substring(0, 2);
String country = languageCode.substring(3, 5);
locale = new Locale(language, country);
}
} else {
locale = new Locale(languageCode);
}
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
refresh();
updatedLocale = true;
}
}
/**
* Hide FirstLaunchPref from preference screen
*
*/
private void hideFirstLaunchPref() {
// First Launch Preference
PreferenceCategory firstlaunchPref = (PreferenceCategory) findPreference(getString(R.string.pref_app_first_launch));
// Hide First Launch Preference
getPreferenceScreen().removePreference(firstlaunchPref);
}
/**
* Set LanguagePref default Values (summary, selected language)
*
*/
private void setLanguagePrefDefaultValues() {
// Current Locale
Locale sysLocale = getResources().getConfiguration().locale;
// Language Preference
ListPreference languagePref = (ListPreference) findPreference(getString(R.string.pref_language_key));
// Edit Preference Summary
languagePref.setSummary(sysLocale.getDisplayName(sysLocale));
// Set Language Preference to Current Locale
if (sysLocale.getCountry() != "") {
languagePref.setValue(sysLocale.getLanguage() + "_" + sysLocale.getCountry());
} else {
languagePref.setValue(sysLocale.getLanguage());
}
}
/**
* Set ProfileEditPref Intent
*
*/
private void setProfileEditPrefIntent() {
// Start PoS Profile Activity onClick
Preference profile = (Preference) findPreference(getString(R.string.pref_profile_edit_key));
profile.setIntent(new Intent(SettingsActivity.this,
PosProfileActivity.class));
}
/**
* Restart activity to reflect locale change
*
*/
private void refresh() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
/**
* Back to previous activity
*
* @param View
* v
*/
public void back(View v) {
finish();
}
}