问题
I'm trying to learn how to do live wallpapers for android. I've got it working and now I want to add a couple preferences. So, I've created a preference activity and followed all the examples I could find.
I can capture ListBoxPreferences just fine, but the 'onSharedPreferenceChanged' method is never called when a checkbox has been changed.
Is there extra code that has to be added for capturing checkbox changes? Is there a best practices way of doing check boxes in preferences?
I've been banging my head against this issue for two days, any help would be GREATLY appreciated!
Thanks!
回答1:
@Josh - Good answer.
I'd like to add for the sake of completeness is that you should set things up so you register for changes in your PreferenceActivity's onResume() method and unregister in the onPause() method.
The answer to this question has a good sample of code taken from on the the stock Android example programs.
Alternatively, you can also register listeners for each of the preferences in your activity's onCreate() and handle events on them separately .
回答2:
Did you register to receive onSharedPreferenceChanged callbacks?
Add this to your main activity's onCreate method (and make sure to implement onSharedPreferenceChanged):
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).registerOnSharedPreferenceChangeListener(this);
来源:https://stackoverflow.com/questions/4260841/how-do-i-capture-changes-of-a-checkboxpreference-in-android-development