我有以下内容创建了包含两个通知设置的设置页面:
SettingsActivity.java
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class NotificationPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_notification2);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
pref_notification2.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="true"
android:key="notifications_team_pick"
android:title="Team Pick Reminders" />
<SwitchPreference
android:defaultValue="true"
android:key="notifications_results"
android:title="Results Notifications" />
</PreferenceScreen>
我希望在按下通知选项时调用以下内容:
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
Log.d("Am I: ", "Here");
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
Log.d("Am I: ", "Here 2");
}
return true;
}
};
然而,无论我尝试什么,一旦按下切换按钮,我就无法发生任何事情。任何帮助将不胜感激,非常感谢,艾伦。
添加以匹配建议:
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class NotificationPreferenceFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("I am at: ", "OnCreate extends");
addPreferencesFromResource(R.xml.pref_notification2);
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
//Your preference change method code.
Log.d("I am at: ", "OnCreate extends Hope");
return true;
}
}
答案 0 :(得分:1)
您需要像这样更改您的片段 -
package com.accelerate.myprefapplication;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
public class MyPreferencesActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.path);
bindPreferenceSummaryToValue(findPreference("notifications_team_pick"));
bindPreferenceSummaryToValue(findPreference("notifications_results"));
}
private void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(this);
// Trigger the listener immediately with the preference's
// current value.
onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getBoolean(preference.getKey(), true));
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean state = Boolean.valueOf(newValue.toString());
Log.d("Am I: ", "Here");
return true;
}
}
}
我认为这应该适合你。
寻找这个答案。
答案 1 :(得分:1)
那不行。您必须检索与交换机交互后存储的值。用它来检索数据。
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String effchoice = prefs.getString("notifications_team_pick", getString(R.string.default_eff));
BTW,您的pref_notification2.xml错误。您尚未设置要保存的值和其他内容。像这样:
<ListPreference
android:dependency="pref_sync"
android:key="pref_syncConnectionType"
android:title="@string/pref_syncConnectionType"
android:dialogTitle="@string/pref_syncConnectionType"
android:entries="@array/pref_syncConnectionTypes_entries"
android:entryValues="@array/pref_syncConnectionTypes_values"
android:defaultValue="@string/pref_syncConnectionTypes_default" />
阅读here