How do I capture changes of a CheckBoxPreference in android development?

隐身守侯 提交于 2019-12-12 04:15:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!