preference经理 [英] preference manager

查看:178
本文介绍了preference经理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过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:

我不得不承认,第一种可能是一个比较笼统的回答。 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

@Override
    public void onCreate(Bundle savedInstanceState){ 
      super.onCreate(savedInstanceState);

      preference();

[...] my code: buttons, textview, etc [...]

private void preferences() {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    CheckboxPreference = prefs.getBoolean("checkboxPref", true);
    ListPreference = prefs.getString("listpref", "");

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆