Android偏好设置(4)设置默认值
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_VALUESin 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)设置默认值的更多相关文章
- 设置easyui input默认值
/*设置input 焦点*/ $(function () { //集体调用 $(".formTextBoxes input").each(function () { $(this) ...
- 二货Mysql中设置字段的默认值问题
Mysql设置字段的默认值的确很落伍 1.不支持函数 2.只支持固定常量. 经常用到的日期类型,因为不支持getdate或者now函数,所以只能设置timestamp类型 而且还必须在默认值那个地方写 ...
- mysql设置timpstamp的默认值为 '0000-00-00 00:00:00' 时报错
问题:mysql设置timpstamp的默认值为 '0000-00-00 00:00:00' 时报错: ERROR 1067 (42000): Invalid default value for 'u ...
- Odoo14 设置Binary字段默认值
1 # Odoo 中的附件也就是Binary字段都是经过特殊处理的 2 # 首先是上传的时候会进行base64编码后再上传到服务器 3 # 服务器进行压缩存放在odoo文件仓库中 4 # 每个odoo ...
- MySQL设置字段的默认值为当前系统时间
问题产生: 当我们在对某个字段进行设置时间默认值,该默认值必须是的当前记录的插入时间,那么就将当前系统时间作为该记录创建的时间. 应用场景: 1.在数据表中,要记录每条数据是什么时候创建的,应该由数据 ...
- [转]Hibernate设置时间戳的默认值和更新时间的自动更新
原文地址:http://blog.csdn.net/sushengmiyan/article/details/50360451 Generated and default property value ...
- jquery 设置select的默认值
<select id="sel" > <option value="s1" > aaaa </option> <opt ...
- Hibernate设置时间戳的默认值和更新时间的自动更新
Generated and default property values 生成的和默认的属性值 The database sometimes generates a property value, ...
- oracle数据库的一个表中,怎么设置字段的默认值
如果表已经存在,用如下方法设置默认值. alter table 表名 modify 字段名 default 默认值; 如test表中设置address字段为'浙江省',可用如下语句: alter ta ...
- mysql使用default来设置字段的默认值
mysql创建表时,我们可以使用default来设置表字段的默认值,这样当向表中插入或添加数据时,如果没有为此字段设置任何值,则使用default默认值来填充该字段的值 在使用create table ...
随机推荐
- Ajax系列之四:问题总结
1.最经典的就是ie下的缓存问题了. 假设使用的是get.那么在ie下出现缓存问题.导致代码仅仅运行一次. 解决的方法就是加时间戳或者随机数,使url变为唯一,这样就不会出现ie 下的缓存问题了, ...
- Unity5的关卡切换
本文章由cartzhang编写,转载请注明出处. 全部权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51055584 作者:car ...
- Tomcat 安装错误
安装tomcat时,遇到"failed to install tomcat6 service check your settings and permissions"的问题 安装时 ...
- os 2大功能
支撑功能 中断管理 时钟管理 原语操作 primitive 资源管理功能 进程管理 存储器管理 设备管理
- cxf与struts2拦截器冲突的解决方案
最近学习接口,学习了下cxf,用tomcat部署访问的时候,发现接口不能访问:百度了很多,最终找到比较好的解决方案: sturts2配置: <!-- 设置strus拦截器 --> < ...
- jQuery 1.x and 2.x , which is better?
1. jQuery 1.x和2.x的区别 或者可以说是jQuery 2.x有什么新特征? jQuery官方发布2.x原话 不再支持IE6/7/8,如果在IE9/10里只用“兼容性视图”模式也将会受到影 ...
- jsonp突破同源策略,实现跨域訪问请求
版权声明:本文为博主原创文章,未经博主同意不得转载.如需转载请声明:[转自 http://blog.csdn.net/xiaoxian8023 ] https://blog.csdn.net/xiao ...
- ABAP ALV F4帮助
ALV F4帮助, 选值保存到ALV. TYPE-POOLS:slis. CLASS lcl_event_receiver DEFINITION DEFERRED. DATA: gt_fcat TYP ...
- oracle经典建表语句--scott建表
create table EMP ( EMPNO ) PRIMARY KEY, ENAME ), JOB ), MGR ), HIREDATE DATE, SAL ,), COMM ,), DEPNO ...
- Hibernate、Spring和Struts2工作原理
Hibernate.Spring和Struts2工作原理 博客分类: Java 基础 工作HibernateSpringMVCStruts Hibernate.Spring和Struts2工作原理 ...