preference经理 [英] preference manager
问题描述
通过preferences.xml的用户选择通过列表preF 4的值。后第一次运行Android preference(),当用户选择通过listfref上又有价值,如何执行preference()?这里我把preference()我的code之内?谢谢!
在MainActivity,我已经
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); preference();
...]
我的code:按钮,TextView的,等
[...]
私人无效preferences(){ 共享preferences preFS = preferenceManager.getDefaultShared preferences(getBaseContext());
复选框preference = prefs.getBoolean(复选框preF,真正的);
列表preference = prefs.getString(名单preF,);
我想你想知道的,你怎么可以得到最新的preferences还是怎么可以,如果用户更改了preference通知。您的preferences()方法查询最新的preferences。随着在OnCreate(...)方法调用preferences您检索最新的preferences建立相应的活动时。 intially检索您的preferences之后,我看到两种可能性,以获得最新的preferences:
- 检索最新的preferences当你需要他们。打电话给你的preferences方法()你需要最新的preferences之前。例如,把它放在你的onUpdate(...)方法,如果你需要最新的preferences那里。
- 成为通知时preferences改变。使用监听器监听preferences的变化。该共享preferences接口定义OnShared preferenceChangeListener,你可以实现并传递给你的共享preferences。
我不得不承认,第一种可能是一个比较笼统的回答。 href=\"http://developer.android.com/guide/topics/data/data-storage.html\" rel=\"nofollow\"> Android开发者的存储选项网站大约有使用共享$更多信息, By preferences.xml an users choose a value from 4 by listpref. After first time that android runs preference(), when an users choose again another value by listfref, how execute preference()? Where I put preference() within my code? thanks! In the MainActivity, I've [...]
my code: buttons, textview, etc
[...]
I suppose you want to know, how you can get the latest preferences or how you can be notified if the user changes a preference. Your preferences() method queries the latest preferences. With calling preferences in the onCreate(...) method you retrieve the latest preferences when the corresponding activity is created. After intially retrieving your preferences I see two possibilities to get the newest preferences: I have to admit that the first possibility is a rather general answer. The android developers storage options site has more information about using shared preferences. 这篇关于preference经理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
preference();
private void preferences() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
CheckboxPreference = prefs.getBoolean("checkboxPref", true);
ListPreference = prefs.getString("listpref", "");