在我的应用的设置页面中有两个EditTextPreference
(s)当我更改值时,必须更新摘要。现在,只要我在EditTextPreference
(s)中输入值并点击确定,它就会立即更新摘要。但是当我重新启动应用程序时,摘要会更改回默认值。但是新值保留在EditTextPreference
(我打开它时可以看到)。当我在EditTextPreference
中输入新值时,摘要会再次更新。
如何每次从用户输入更新摘要?
这是我的代码:
SettingActivity.java
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment()).commit();
setTitle("Settings");
ActionBar actionBar=getSupportActionBar();
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings_layout);
setHasOptionsMenu(true);
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Preference pref = findPreference(key);
if (pref instanceof EditTextPreference) {
EditTextPreference listPref = (EditTextPreference) pref;
pref.setSummary(listPref.getText());
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), HomeActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
settings_layout.xml
<EditTextPreference
android:capitalize="words"
android:dependency="email_switch"
android:defaultValue="@string/default_email_address"
android:key="example_text"
android:summary="@string/pref_description_email"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_email_address_text_title"
android:inputType="textWebEmailAddress" />
<EditTextPreference
android:capitalize="words"
android:dependency="sms_switch"
android:defaultValue="@string/default_mobile_number"
android:key="example_text1"
android:summary="@string/pref_description_sms"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_mobile_number_text_title"
android:inputType="phone"/>
P.S。我在我的MAIN活动中将其称为
PreferenceManager.setDefaultValues(this, R.xml.settings_layout, false);
答案 0 :(得分:1)
在onCreate()
课程的SettingsFragment
方法中,您需要获得对SharedPreferences
的引用,然后运行onSharedPreferenceChanged()
方法。您的onCreate()
应如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings_layout);
setHasOptionsMenu(true);
//if you are using default SharedPreferences
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
onSharedPreferenceChanged(sharedPrefs, getString(R.string.key1));
onSharedPreferenceChanged(sharedPrefs, getString(R.string.key2));
}
key1
和key2
是您的偏好键(在您的情况下为example_text
和example_text1
)保存到strings
文件中。我自己做这个没有字符串引用的问题,这就是为什么我建议提取它们。