preference经理

编程入门 行业动态 更新时间:2024-10-23 07:22:56
本文介绍了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:

  • 检索最新的preferences当你需要他们。打电话给你的preferences方法()你需要最新的preferences之前。例如,把它放在你的onUpdate(...)方法,如果你需要最新的preferences那里。
  • 成为通知时preferences改变。使用监听器监听preferences的变化。该共享preferences接口定义OnShared preferenceChangeListener,你可以实现并传递给你的共享preferences。

我不得不承认,第一种可能是一个比较笼统的回答。 href=\"developer.android/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:

  • Retrieve the latest preferences when you need them. Call your preferences method() just before you are needing the latest preferences. For example, put it in your onUpdate(...) method if you need latest preferences there.
  • Become notified when the preferences are changed. Use a listener to listen for changes of the preferences. The SharedPreferences interface defines the OnSharedPreferenceChangeListener which you can implement and pass to your SharedPreferences.

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经理

本文发布于:2023-11-25 13:55:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630026.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:经理   preference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!