android - onPreferenceClick and OnPreferenceClickListener -
i'm attempting evaluate preferences in java code in order enable/disable other options chose not other options... far i'm trying implement onpreferenceclicklistener never see toast changes. doing wrong? there seem alot of other questions cannot see error in reference them.
public class usersettingactivity extends preferenceactivity implements onpreferenceclicklistener{ sharedpreferences mpreferences; boolean frequency; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.settings); } @override public boolean onpreferenceclick(preference preference) { mpreferences = preferencemanager.getdefaultsharedpreferences(this); frequency = mpreferences.getboolean("frequency", true); context context = getapplicationcontext(); toast.maketext(context, "hello toast 0!", toast.length_long).show(); if (!frequency) { context context2 = getapplicationcontext(); toast.maketext(context2, "hello toast 1!", toast.length_long).show(); } else if (preference.getkey().equals("schedulestop")) { } else if (preference.getkey().equals("priority")) { } else { context context3 = getapplicationcontext(); toast.maketext(context3, "hello toast 0!", toast.length_long).show(); } return false; } }
you have register preferenceclicklistener each individual preference
somepreference.setonpreferenceclicklistener(this);
or can use getsharedpreferences().registeronsharedpreferencechangelistener
preferences.
public class usersettingactivity extends preferenceactivity implements onsharedpreferencechangelistener{ sharedpreferences mpreferences; boolean frequency; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.settings); } @suppresswarnings("deprecation") @override protected void onpause() { super.onpause(); getpreferencescreen().getsharedpreferences().unregisteronsharedpreferencechangelistener(this); } @suppresswarnings("deprecation") @override protected void onresume() { super.onresume(); getpreferencescreen().getsharedpreferences().registeronsharedpreferencechangelistener(this); } @suppresswarnings("deprecation") @override public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) { if (key.equals("schedulestop")) { // } else if (key.equals(...... } }
Comments
Post a Comment