Preference的使用(1) --- 基础API的介绍

SharedPreference  我想大家使数据持久化的时候都使用过,但是现在讲的不是它而是 Preference 

先介绍一下 , 下述介绍是英文翻译过来的,翻译不好别喷哦:

代表着一种基本的Preference的UI构件,是以列表视图显示的(相当于我们的Activity中主布局是一个ListView的那种UI显示)
这种类提供的视图在Activity中显示,并且跟SharePreference的存/取有关系
当指定使用XML的层次架构的时候,每一个元素都是preference的子类,跟视图的层次跟布局类似
这个类中包含一个Key值,这个key值跟SharedPreference的存储键有关系。具体怎么存是跟子类有关系


1.创建一个Preference的布局

先创建一个Preference的布局,让大家看一下效果:

它的Activity要继续PreferenceActivity ,并且使用addPreferencesFromResource(xxxx);来添加Preference

代码:

@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.text_preference);
	}
资源文件要先建立一个xml的文件夹
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <Preference
        android:key="preference0"
        android:summary="preference0"
        android:title="preference" >
    </Preference>
    
    <Preference
        android:key="preference1"
        android:summary="preference1"
        android:title="preference1" >
    </Preference>

</PreferenceScreen>
先看一下效果:


先看XML中的这个重要的3项。

根Layout是PreferenceScreen,这个不解析,后面会有

Preference中的key  是通过key来找到的这个Preference的
title就是这个Preference的标题, <pre name="code" class="java">summary  就是描述信息,现在在标题的下面

 

2.API的介绍 --- 这个才是重点

1. int compareTo(Preference another)

    如果设置了Preference的order值,就按照preference的order值来排序,默认值是0,否则会按照字母的值来排序

    返回值:0  相同的级别

    <0 排在another之前    

    > 0 排在another之后

    这个order是什么呢,接下来看:

2.setOrder(int order)

   在同一级的preference中,根据order的值进行排序,从小到大的排序

   这个是什么意思呢,所谓的同一级我的理解是 在同一个Preference的根下面,例如现在的

preference0 跟 preference1  都是在同一个根PreferenceScreen下面

   order值默认的值是多少呢,有setOrder的方法那么就会有getOrder的方法,可以打印出来看一下:

@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.text_preference);
		findPreference();
	}

	@SuppressWarnings("deprecation")
	private void findPreference() {
		preference0 = findPreference("preference0");
		preference1 = findPreference("preference1");
		
		Log.i(TAG , " default order = " + preference0.getOrder());
		
	
	}
findPreference(key) 是通过XMl中定义的来找到对应的值,

     打印结果  ----------   default order = 0

那么默认值为0 ,    那么2个order都是0 , 上面说使用从小到大的排序,那么把

preference0 的order设置为比0 大的值的话,是不是就在下面了呢,那么就代码试验一下:

    preference0.setOrder(2);  

效果:

   

果然是这个样的由此,order 就牵扯到同级排序而已。

这个值可以通过xml来指定

 <Preference
        android:order="1"
        android:key="preference0"
        android:summary="preference0"
        android:title="preference" 
        >
    </Preference>
3. int getOrder()   得到其order值

4.Context getContext() 返回preference的context对象

5.String getDependency() 返回它的依赖 ,不是很清楚是个什么东西

6.Editor  getEditor() 得到它对应的sharedPreference的editor 用来 put 跟  commit

7.public void setIntent(Intent intent)   设置Intent ,当点击的时候就startActivity(intent)

这样一个preference被点击时直接调用,相当于view的 onClick事件的时候启动一个intent

这个Intent 可以在Xml中指定 格式如下:

 
    <Preference
        android:order="2"
        android:key="preference1"
        android:summary="preference1"
        android:title="preference1" >
        <intent
            android:targetClass="com.yunos.osupdate.front.UpdateActivity"
            android:targetPackage="com.yunos.osupdate"
             />
    </Preference>

8. public Intent getIntent ()返回最后一次被设置的Intent  或者在XML中设置的Intent

9.public void setLayoutResource(int layoutResId)  这个看字面意思就是设置自定义布局的preference

大多数情况下使用默认布局就OK了 ,使用自定义布局时应该要包含:

android.R.id#widget_frame  android.R.id#title  android.R.id#summary等

也就是说我们自定义时   如果要有title 那么id 必须是android.R.id.title

先看一下系统的默认布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingEnd="?android:attr/scrollbarSize"
    android:background="?android:attr/selectableItemBackground" >

    <ImageView
        android:id="@+android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dip"
        android:layout_marginEnd="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" />

</LinearLayout>
查看其布局可以看到在左边还可以发一个icon  然后是 title  跟  summary   ,然后加上一个widget_frame 暂时不知道是干什么用的,是一个垂直的线性布局


那么我们现在来自定义一个布局:

先使用系统用默认的布局修改一下吧:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingEnd="?android:attr/scrollbarSize"
    >

    <ImageView
        android:id="@+android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher"
        />
    
    <ImageView
        android:id="@+android:id/icon1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher"
        />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dip"
        android:layout_marginEnd="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#ff00ff00"
            android:maxLines="4" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:background="#ff00ffff" />

</LinearLayout>


效果如下,只是把字体颜色改变了而且增加了一个icon1 ,还有要注意的事
 <ImageView
        android:id="@+android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher"
        />

虽然设置的src,但是还是没有显示的,因为是  @+idandroid:id/icon 是系统的布局,要在XML的引用中设置才可以:也就是icon重新被设置了,title  跟 summary都是同理的

 <Preference
        android:order="1"
        android:key="preference0"
        android:summary="preference0"
        android:title="preference" 
        >
    </Preference>
中加上icon才可以显示出来如,也就是说:
<span style="font-size:18px;"><Preference
        android:order="1"
        android:key="preference0"
        android:summary="preference0"
        android:title="preference" 
        </span><strong><span style="font-size:32px;">android:icon="@drawable/ic_launcher"</span></strong><span style="font-size:18px;">
        >
    </Preference></span>
那么结果就是这样,

API中有提到布局文件中要有

@+android:id/title 类似的id ,如果没有的话会怎么样呢,结果就是  setTitle是没有作用的,同理  setSummay等也是没有作用的
把Xml文件中<pre name="code" class="java">@+android:id/title   修改为一个系统不认识的或者不写,那么结果就是如图:<img src="https://img-blog.csdn.net/20141120150135820?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGV3ZW5jZTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

就是调用setTitle也是无用的,这个不难理解。系统的XMl解析 跟settile都跟根据id来的Id找不到,当然也就没有什么所谓的title了
知道了这个,自定义布局就没有什么要注意的啦,把它完成当做list的item就是了

 

10.public int getLayoutResource()  返回其资源文件的ID

11.public void setWidgetLayoutResource(int widgetLayoutResId)

看到这个东西就想到第九点中的

LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:background="#ff00ffff" />
这个默认是空的,那么通过这个方法来填充这个部分的布局:
那么自定义一个:
  <pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <TextView 
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="widget context "/>

</RelativeLayout>
把自定义的preference的布局也全部贴一下代码:
<pre name="code" class="java"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:descendantFocusability="blocksDescendants"
    
    >

    <ImageView
        android:id="@+android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher"
        />
    
    <ImageView
        android:id="@+android:id/icon1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher"
        />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dip"
        android:layout_marginEnd="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#ff00ff00"
            android:maxLines="4" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:background="#ff00ffff" />
    
  
</LinearLayout>

运行结果是:

 
<img src="https://img-blog.csdn.net/20141120152930171?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGV3ZW5jZTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
12.<span style="font-family: Arial, Helvetica, sans-serif;">public int </span><a target=_blank href="" style="font-family: Arial, Helvetica, sans-serif;"><strong>getWidgetLayoutResource</strong></a><span style="font-family: Arial, Helvetica, sans-serif;">()  widget的资源文件的ID</span>
<span style="font-family: Arial, Helvetica, sans-serif;">13.</span><span style="font-family: Arial, Helvetica, sans-serif;">public </span><a target=_blank title="android.view 中的类" href="" style="font-family: Arial, Helvetica, sans-serif;">View</a><span style="font-family: Arial, Helvetica, sans-serif;"> </span><a target=_blank href="" style="font-family: Arial, Helvetica, sans-serif;"><strong>getView</strong></a><span style="font-family: Arial, Helvetica, sans-serif;">(</span><a target=_blank title="android.view 中的类" href="" style="font-family: Arial, Helvetica, sans-serif;">View</a><span style="font-family: Arial, Helvetica, sans-serif;"> convertView,</span><a target=_blank title="android.view 中的类" href="" style="font-family: Arial, Helvetica, sans-serif;">ViewGroup</a><span style="font-family: Arial, Helvetica, sans-serif;"> parent)  返回在activity中显示的view </span>
<span style="font-family: Arial, Helvetica, sans-serif;">14.setTitle   setSummary  getTitle   getSummary  看名字就知道是什么意思了</span>
<span style="font-family: Arial, Helvetica, sans-serif;">15.</span><span style="font-family: Arial, Helvetica, sans-serif;">public void </span><a target=_blank href="" style="font-family: Arial, Helvetica, sans-serif;"><strong>setEnabled</strong></a><span style="font-family: Arial, Helvetica, sans-serif;">(boolean enabled)  设置是否可以enable ,如果false那么不能响应click事件</span>
<span style="font-family: Arial, Helvetica, sans-serif;">16.</span><span style="font-family: Arial, Helvetica, sans-serif;">public boolean </span><a target=_blank href="" style="font-family: Arial, Helvetica, sans-serif;"><strong>isEnabled</strong></a><span style="font-family: Arial, Helvetica, sans-serif;">() 检测是否enable</span>
17. public void setSelectable (boolean selectable) 设置是否可以被选择,如果false,那么不能被选中,也不可以被点击了
 
<span style="font-family: Arial, Helvetica, sans-serif;">18.</span><span style="font-family: Arial, Helvetica, sans-serif;">public boolean </span><a target=_blank href="" style="font-family: Arial, Helvetica, sans-serif;"><strong>isSelectable</strong></a><span style="font-family: Arial, Helvetica, sans-serif;">()   是否可以被选择</span>
<span style="font-family: Arial, Helvetica, sans-serif;">19.</span><span style="font-family: Arial, Helvetica, sans-serif;">public boolean </span><a target=_blank href="" style="font-family: Arial, Helvetica, sans-serif;"><strong>getShouldDisableView</strong></a><span style="font-family: Arial, Helvetica, sans-serif;">()  检测preference是否禁用了它的View</span>
<span style="font-family: Arial, Helvetica, sans-serif;">20.</span><span style="font-family: Arial, Helvetica, sans-serif;">public void </span><a target=_blank href="" style="font-family: Arial, Helvetica, sans-serif;"><strong>setShouldDisableView</strong></a><span style="font-family: Arial, Helvetica, sans-serif;">(boolean shouldDisableView)   设置是否被禁用,当它处于disabled状态的时候,如果设置为true ,那么将禁用这个View</span>
<span style="font-family:Arial, Helvetica, sans-serif;">19跟 20 有点抽象不要理解,试验一下:</span>
<span style="font-family:Arial, Helvetica, sans-serif;">首先要在disable状态下 ,然后在</span><a target=_blank href="http://write.blog.csdn.net/postedit?ref=toolbar" style="font-family: Arial, Helvetica, sans-serif; font-weight: bold;">setShouldDisableView</a><span style="font-family: Arial, Helvetica, sans-serif; font-weight: bold;">(false)</span>
<span style="font-family: Arial, Helvetica, sans-serif; font-weight: bold;">看一下结果:</span>
<span style="font-family: Arial, Helvetica, sans-serif; font-weight: bold;">代码如下:</span><pre name="code" class="java">Log.i(TAG , "=== before ShouldDisableView = " + preference0.getShouldDisableView());
		preference0.setEnabled(false);
		Log.i(TAG , "===After ShouldDisableView = " + preference0.getShouldDisableView());
		preference0.setShouldDisableView(false);
		Log.i(TAG , "===After ShouldDisableView = " + preference0.getShouldDisableView());
截图:
 
<span style="font-family: Arial, Helvetica, sans-serif; font-weight: bold;"><img src="https://img-blog.csdn.net/20141120160547500?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGV3ZW5jZTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</span>
<span style="font-family:Arial, Helvetica, sans-serif;"><strong><span style="font-size:18px;">打印信息为:<img src="https://img-blog.csdn.net/20141120160643143?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGV3ZW5jZTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  原来</span></strong></span><pre name="code" class="java" style="font-size:18px; font-weight: bold;">getShouldDisableView()默认是为真的,那么现在的现象我描述一下:
 1. preference是无法响应点击事件的
<span style="font-size:18px;">2. prefence上的button0 跟 button 点击是有效的</span><span style="font-size:18px; white-space: pre; background-color: rgb(240, 240, 240);"></span><pre name="code" class="java" style="font-size:18px;">
现在set为true看一下:
<img src="https://img-blog.csdn.net/20141120161136234?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGV3ZW5jZTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
结果大家看到了,其实set为true没有必要的,因为默认就是true
<pre name="code" class="java"><span style="font-family:Arial, Helvetica, sans-serif;"><strong></strong></span><pre name="code" class="java">etShouldDisableView()默认是为真的,那么现在的现象我描述一下:
 1. preference是无法响应点击事件的
2. prefence上的button0 跟 button 点击也是无效的
 

 
如果把setEnable为true的 那么setShouldDisaleView是没有意义的

21. getKey  setKey  hasKey  从名字就可以知道其意思 略
<span style="font-size:18px;">22. </span><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">public boolean </span><a target=_blank href="" style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">isPersistent</a><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">()</span>
 
 

<span style="font-family:Arial, Helvetica, sans-serif;"><strong></strong></span><pre name="code" class="java" style="font-weight: bold;"><pre name="code" class="java"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><span style="font-size:18px;">
</span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"> 是否是持久化的,如果是真那么值会保存在sharedPreference中,它的子类有值,如checkBoxpreference</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;font-size:18px;">23.</span><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;">public void <a target=_blank href="">setPersistent</a>(boolean persistent)  设置为真,值会保存在sharepreference中</span>
 
<span style="font-family:Arial, Helvetica, sans-serif;font-size:18px;"></span><h3 style="display: inline !important;"></h3>
 
<strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;">24.</span>
 
<strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;">public void <a target=_blank href="">setOnPreferenceChangeListener</a>(<a target=_blank title="android.preference 中的接口" href="">Preference.OnPreferenceChangeListener</a> onPreferenceChangeListener)
 
 
 
 
<strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;">preference的值被改变的时候的回调
 
 
 
 
<strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;">25.
 
 
 
<strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;">public <a target=_blank title="android.preference 中的接口" href="">Preference.OnPreferenceChangeListener</a> <a target=_blank href="">getOnPreferenceChangeListener</a>()  得到其接口回调
 
 
 
 
<strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><pre>26.<strong style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;">public void <a target=_blank href="">setOnPreferenceClickListener</a>(<a target=_blank title="android.preference 中的接口" href="">Preference.OnPreferenceClickListener</a> onPreferenceClickListener
 
 
 
 
<strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><dl style="display: inline !important;"><dd style="display: inline !important;">被点击时的回调</dd><dd style="display: inline !important;"><pre><strong style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><dl style="display: inline !important;"><dd style="display: inline !important;">27.<pre>public <a target=_blank title="android.preference 中的接口" href="">Preference.OnPreferenceClickListener</a> <a target=_blank href=""><strong>getOnPreferenceClickListener</strong></a>()  <span style="white-space: pre; background-color: rgb(240, 240, 240);"></span><pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;">得到其点击接口回调
 
 
 
 
 
 
<pre name="code" class="java" style="display: inline !important;"><pre name="code" class="java" style="display: inline !important;"><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><pre style="display: inline !important;">28.
 
 
 
 
 
 
 
 
<pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><dl style="display: inline !important;"><dd style="display: inline !important;"><pre style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><dl style="display: inline !important;"><dd style="display: inline !important;"><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><h3 style="display: inline !important;"><pre style="display: inline !important;"><a target=_blank title="android.content 中的接口" href="">SharedPreferences</a> <a target=_blank href="">getSharedPreferences</a>()

 
 
 
<pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><dl style="display: inline !important;"><dd style="display: inline !important;"><pre style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><dl style="display: inline !important;"><dd style="display: inline !important;"><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><h3 style="display: inline !important;"><pre style="display: inline !important;">得到对应的sharedPreference

 
 
 
<pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><dl style="display: inline !important;"><dd style="display: inline !important;"><pre style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><strong style="font-family: Arial, Helvetica, sans-serif;"></strong><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><dl style="display: inline !important;"><dd style="display: inline !important;"><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><h3 style="display: inline !important;"><pre style="display: inline !important;">29.<pre>public boolean <a target=_blank href=""><strong>shouldCommit</strong></a>()
 
 

 
 
 
是否可以被commit
29.
public PreferenceManager getPreferenceManager()  得到其manager
30.
public void notifyDependencyChange(boolean disableDependents)
是否禁止其依赖     现在还不清楚  依赖指的是啥呢么
31.
public void onDependencyChanged(Preference dependency,
                                boolean disableDependent)  当依赖被改变时调用
  
  
dependency - 它所依赖的preference
disableDependent - 设置为true 将disable 它依赖的preference

 
 
 
 
 
 
 
 
 
 
 
32.
public boolean shouldDisableDependents()  它所依赖的preference 是否可以是被disabled,为true的话,那么依赖是disbale的
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33. 
public void setDependency(String dependencyKey)  设置它的依赖(根据Key值)
34. 
public String getDependency()  得到其依赖的Key
从后面看,preference可以设置一种依赖关系.
 
35.
public void saveHierarchyState(Bundle container)
使用bundle 保存preference的状态信息
36.
public void restoreHierarchyState(Bundle container)  恢复以前保存的状态信息
 
对于依赖关系,如果A 依赖与B  那么B可用(enable) ,那么A也可用   B不可以用那么A也不可以用  
 
preference0.setEnabled(true);
preference1.setDependency("preference0");  那么
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></span><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></span><pre style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"></span></span><pre><span style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></span><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></span><pre style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"></span></span><dl style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><dd style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre><span style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></span><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></span><pre style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"></span></span><dl style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><dd style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"></span><strong></strong></span><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><strong><strong></strong><strong></strong></strong></span><pre style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><strong><span style="font-size:18px;"></span></strong></span><dl style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></span><dd style="display: inline !important;"><strong></strong><pre style="display: inline !important;"><strong><span style="font-family: Arial, Helvetica, sans-serif;"></span></strong><pre name="code" class="java" style="display: inline !important;"><strong><span style="font-family: Arial, Helvetica, sans-serif;"></span><span style="font-family: Arial, Helvetica, sans-serif;"></span></strong><pre style="display: inline !important;"><strong><span style="font-size:18px;"></span></strong><dl style="display: inline !important;"><strong></strong><dd style="display: inline !important;"><strong></strong><pre name="code" class="java"><strong></strong><pre name="code" class="java" style="display: inline !important;"><strong></strong><pre style="display: inline !important;"><strong><span style="font-size:18px;"></span><span style="font-family: Arial, Helvetica, sans-serif;"></span></strong><pre name="code" class="java" style="display: inline !important;"><strong><span style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></span></strong><pre name="code" class="java" style="display: inline !important;"><strong><span style="font-family: Arial, Helvetica, sans-serif;"></span></strong><pre style="display: inline !important;"><strong><span style="font-size:18px;"></span></strong><dl style="display: inline !important;"><strong></strong><dd style="display: inline !important;"><strong></strong><pre style="display: inline !important;"><strong><span style="font-family: Arial, Helvetica, sans-serif;"></span></strong><pre name="code" class="java" style="display: inline !important;"><strong><span style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></span></strong><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><strong></strong></span><pre style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"></span></span><dl style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><dd style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></span><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"><strong><strong></strong></strong></span><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><dl style="display: inline !important;"><dd style="display: inline !important;"><pre style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></span><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java" style="display: inline !important;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java" style="display: inline !important;"><pre style="display: inline !important;"><span style="font-size:18px;"></span><pre style="display: inline !important;"><dl style="display: inline !important;"><dd style="display: inline !important;"><pre>preference0  跟 preference1 的状态是一样的 ,除非preference1 重新设置了enable
也可以使用Xml代码来设置:
<Preference
        android:order="2"
        android:key="preference1"
        android:summary="preference1"
        android:dependency="preference0"
        android:title="preference1" >
        <intent
            android:targetClass="com.yunos.osupdate.front.UpdateActivity"
            android:targetPackage="com.yunos.osupdate"
             />
    </Preference>
3.子类有哪些?
下节分析

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值