开发者

Preferences layout for non preferneces data

I have an activity which is used for editing some object. Currently its layout as a simple form but I'd like it to be more similar to preferences activity. I'd like to use the same layouts which are available there (e.g. CheckBoxPreference) but s开发者_运维百科ave changes to my object rather than to preferences. Is there an easy way to have an activity which looks like preferences but doesn't use preferences at the back back-end?


You can achieve this using next 2 steps:

  1. Set preference to be not persistent via android:persistent="false" in xml or via setPersistent(false) in code. This way default SharedPreferences will not be modified when user changes values in preference UI.

  2. Handle value changes via OnPreferenceChangeListener listener registered using setOnPreferenceClickListener. This will allow you to intercept changes and handle them the way you need.


Yes, you can provide your own implementation of SharedPreferences and save your data to somewhere that is not preferences, while still using the same UI and XML definition. See this question, and this link. The sample link is pretty complex since it is writing data back to the database directly, but it's very easy to replace with a plain Map and then retrieve the data from the Map into your object. Or, if you want the data saved immediately, just write it straight into your object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜