Setting Default Values


The preferences you create probably define some important behaviors for your application, so it's necessary that you initialize the associated SharedPreferences file with default values for each Preference when the user first opens your application.

The first thing you must do is specify a default value for each Preference object in your XML file using theandroid:defaultValue attribute. The value can be any data type that is appropriate for the correspondingPreference object. For example:

<!-- default value is a boolean -->
<CheckBoxPreference
android:defaultValue="true"
... /> <!-- default value is a string -->
<ListPreference
android:defaultValue="@string/pref_syncConnectionTypes_default"
... />

Then, from the onCreate() method in your application's main activity—and in any other activity through which the user may enter your application for the first time—call setDefaultValues():

PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);

Calling this during onCreate() ensures that your application is properly initialized with default settings, which your application might need to read in order to determine some behaviors (such as whether to download data while on a cellular network).

This method takes three arguments:

  • Your application Context.
  • The resource ID for the preference XML file for which you want to set the default values.
  • A boolean indicating whether the default values should be set more than once.

    When false, the system sets the default values only if this method has never been called in the past (or theKEY_HAS_SET_DEFAULT_VALUES in the default value shared preferences file is false).

As long as you set the third argument to false, you can safely call this method every time your activity starts without overriding the user's saved preferences by resetting them to the defaults. However, if you set it to true, you will override any previous values with the defaults.

Android偏好设置(4)设置默认值的更多相关文章

  1. 设置easyui input默认值

    /*设置input 焦点*/ $(function () { //集体调用 $(".formTextBoxes input").each(function () { $(this) ...

  2. 二货Mysql中设置字段的默认值问题

    Mysql设置字段的默认值的确很落伍 1.不支持函数 2.只支持固定常量. 经常用到的日期类型,因为不支持getdate或者now函数,所以只能设置timestamp类型 而且还必须在默认值那个地方写 ...

  3. mysql设置timpstamp的默认值为 '0000-00-00 00:00:00' 时报错

    问题:mysql设置timpstamp的默认值为 '0000-00-00 00:00:00' 时报错: ERROR 1067 (42000): Invalid default value for 'u ...

  4. Odoo14 设置Binary字段默认值

    1 # Odoo 中的附件也就是Binary字段都是经过特殊处理的 2 # 首先是上传的时候会进行base64编码后再上传到服务器 3 # 服务器进行压缩存放在odoo文件仓库中 4 # 每个odoo ...

  5. MySQL设置字段的默认值为当前系统时间

    问题产生: 当我们在对某个字段进行设置时间默认值,该默认值必须是的当前记录的插入时间,那么就将当前系统时间作为该记录创建的时间. 应用场景: 1.在数据表中,要记录每条数据是什么时候创建的,应该由数据 ...

  6. [转]Hibernate设置时间戳的默认值和更新时间的自动更新

    原文地址:http://blog.csdn.net/sushengmiyan/article/details/50360451 Generated and default property value ...

  7. jquery 设置select的默认值

    <select id="sel" > <option value="s1" > aaaa </option> <opt ...

  8. Hibernate设置时间戳的默认值和更新时间的自动更新

    Generated and default property values 生成的和默认的属性值 The database sometimes generates a property value, ...

  9. oracle数据库的一个表中,怎么设置字段的默认值

    如果表已经存在,用如下方法设置默认值. alter table 表名 modify 字段名 default 默认值; 如test表中设置address字段为'浙江省',可用如下语句: alter ta ...

  10. mysql使用default来设置字段的默认值

    mysql创建表时,我们可以使用default来设置表字段的默认值,这样当向表中插入或添加数据时,如果没有为此字段设置任何值,则使用default默认值来填充该字段的值 在使用create table ...

随机推荐

  1. influxDB系列(二)

    来源于我在一个influxDB的qq交流群中的提问, 然后有个人 提了一个问题---->触发了我的思考!! :) 哈哈 自己的每一次说出一个回答,都是一次新的思考,也都进行了一些查阅资料,思考, ...

  2. 使用POI操作Excel时new XSSFWorkbook ()报错java.lang.NoSuchMethodError解决方式

    使用最新的POI3.11时,在执行 Workbook  workBook = new XSSFWorkbook ();这段代码时出现错误: java.lang.NoSuchMethodError: j ...

  3. AnkhSVN介绍

    AnkhSVN介绍 Posted on 2012-11-15 23:24 ArRan 阅读(3120) 评论(1) 编辑 收藏 AnkhSVN是一款在VS中管理Subversion的插件,您可以在VS ...

  4. linux系统编程:线程同步-相互排斥量(mutex)

    线程同步-相互排斥量(mutex) 线程同步 多个线程同一时候訪问共享数据时可能会冲突,于是须要实现线程同步. 一个线程冲突的演示样例 #include <stdio.h> #includ ...

  5. 深度学习笔记之关于基本思想、浅层学习、Neural Network和训练过程(三)

    不多说,直接上干货! 五.Deep Learning的基本思想 假设我们有一个系统S,它有n层(S1,…Sn),它的输入是I,输出是O,形象地表示为: I =>S1=>S2=>….. ...

  6. 下载Youku视频观看

    所需工具: 1.chorme内核浏览器,如Chorme.Firefox等等 2.vlc视频播放器 准备工作完成,开始工作 1.打开优酷的随便一个视频 2.按下F12选择Network(网络)选择Med ...

  7. [IT学习]从网上获取pdf制作vce文件

    考过IT证书的朋友,都知道什么是vce文件.如果仅仅找到了pdf版本的文件,该如何转为vce文件呢? 具体的步骤如下: 1.到如下网址下载examformatter,http://www.examco ...

  8. Speech Recognition Grammar Specification Version 1.0 JavaScript TTS 文本发音

    Speech Recognition Grammar Specification Version 1.0 https://www.w3.org/TR/speech-grammar/ W3C Recom ...

  9. mongodb06---索引

    索引(就像书的目录,先找大致的位置再细找,不是从头开始找): mongodb每行的列可以完全不同,没有列的概念. 索引作用类型: :单列索引 :多列索引 :子文档索引 索引性质: :普通索引 .唯一索 ...

  10. SQL Server 多库操作 库名.dbo.表名 出错的问题!

    SQL Server 多库操作 库名.dbo.表名 出错的问题! 数据库名不要用数字开头. 例如:343934.dbo.user 这就会出错.md a343934.dbo.user 就没问题!! 记住 ...