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_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.

时间: 2024-10-10 06:36:21

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

动态设置radio的默认值

根据后台传值动态设置radion的默认值,一般设置默认值的标签为:checked="checked".具体动态设置默认值得代码如下: <tr> <td width="150" height="30" align="right">性  别:</td> <td width="250" align="left"> <input type=&

js动态设置select的默认值

根据需求动态的设置select的默认值,若要选定默认则代码selected="selected".动态设置则如下所示: <html> <table> <tr> <td width="250" align="left"> <select name="job" size="1" > <option value="普通会员"&g

mysql设置timpstamp的默认值为 &#39;0000-00-00 00:00:00&#39; 时报错

问题:mysql设置timpstamp的默认值为 '0000-00-00 00:00:00' 时报错: ERROR 1067 (42000): Invalid default value for 'updateaDate' 原因和解决方法: # sql-mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_

js设置函数参数默认值的3种方法

js默认是不支持funtion f($a=a){}这种写法的,如果想设置函数参数的默认值,可以参考如下三种方法: 第一种方法: 使用arguments,函数所有的参数都会存入arguments数组离去,所以我们可以从中获取相应的参数然后赋值 function example(a,b){ var a = arguments[0] ? arguments[0] : 1;//设置参数a的默认值为1 var b = arguments[1] ? arguments[1] : 2;//设置参数b的默认值为

JavaScript基础 window.prompt() 弹出一个输入框 设置提示信息与默认值

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=ut

Hibernate Annotation 设置字段的默认值

很简单,不过有点莫名其妙的意思 @Column(name="powerLoad",columnDefinition="bit(1) default 1 ") private boolean load = true; 其实就是在 @Column中加入 columnDefinition属性 里面的值,就是你的字段的SQL中,接在字段名后面的描述 (也有人叫ddl) 上面是布尔类型的,字符型的如下: columnDefinition="varchar(255)

用sql设置access的默认值

http://www.jb51.net/article/990.htm http://m.blog.csdn.net/blog/xijupony/5741707 http://linux.chinaunix.net/techdoc/database/2005/12/14/925763.shtml http://www.cnblogs.com/luckboy/archive/2009/11/21/1607564.html

Extjs combobox设置默认值

转载:http://www.54mask.com/extjs-combobox-default-value.html 相信很多人都遇到了在ExtJS框架中设置combo组件默认值的需求,ExtJS框架并没有提供现成的配置项或者方法来解决此问题,本人认为主要是因为此种需求的应用场景有限且很难达到适应于不同场景的实现. combo组件的设值其实很简单: 1 var getRecruitmentName = new Ext.data.JsonStore({ 2 url: '',--后台地址 3 bas

给yii2框架的dropDownList设置默认值

Yii框架自身已经集成了html表单的组件,比如使用ActiveForm组件可以很方便和Model结合使用,其中下拉框dropDownList到底该怎么样地去给定一个默认值呢? 先在控制器中实例化模型. 比如我们在视图页面中写好一个dropDownList来表示生成一个下拉框select. 1 <?=$form->field($model,'status')->dropDownList(['1'   =>'开启','0'  =>'停用'],['class'  =>'se

001. 为input type=text 时设置默认值

1. 前端HTML代码 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3