我使用 PreferenceFragment 和 OnChangeListener 实现了一个首选项屏幕,以将当前值显示为摘要。以下代码可以正常工作,但是这样做我需要一个 OnChangeListener 用于每个首选项。
SettingsActivity.java
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class UserSettingActivity extends PreferenceActivity {
public static final String PREF_EXCERCISES = "pref_number_of_exercises";
public static final String PREF_TASKS = "pref_number_of_tasks";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//add the settings.xml layout
addPreferencesFromResource(R.xml.settings);
// update the preferences if they change
setUpdatePreferences();
}
private void setUpdatePreferences() {
//get the specified preferences using the key declared in preferences.xml
ListPreference dataPrefConType = (ListPreference) findPreference(PREF_EXCERCISES);
//get the description from the selected item
dataPrefConType.setSummary( dataPrefConType.getEntry() );
//when the user choose other item the description changes too with the selected item
dataPrefConType.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
preference.setSummary(o.toString());
return true;
}
});
}
}
}
首选项.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:defaultValue="15"
android:enabled="true"
android:key="pref_number_of_exercises"
android:numeric="integer"
android:title="Number of exercises" />
<EditTextPreference
android:defaultValue="15"
android:enabled="true"
android:key="pref_number_of_tasks"
android:numeric="integer"
android:title="Number of tasks" />
</PreferenceScreen>
因为它是相同的任务(在摘要中显示值),所以我想为所有偏好设置一个 OnChangeListener - 而且代码更容易阅读。
在这个 stackoverflow 帖子中,他们为所有人使用了一个 OnSharedPreferenceChangeListener。但是,当我将 SettingsActivity.java 更改为以下代码时,应用程序会通过打开首选项屏幕而崩溃。“不幸的是,PreferenceExample 已停止。”
SettingsActivity.java
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.util.Log;
public class UserSettingActivity extends PreferenceActivity {
public static final String PREF_EXCERCISES = "pref_number_of_exercises";
public static final String PREF_TASKS = "pref_number_of_tasks";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
implements OnSharedPreferenceChangeListener {
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//add the settings.xml layout
addPreferencesFromResource(R.xml.settings);
// update the preferences if they change
//setUpdatePreferences();
}
@Override
public void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
Log.i("settings", "preference changed: " + key);
}
}
}