public class Preference
extends Object implements Comparable<Preference>
| java.lang.Object | |
| android.support.v7.preference.Preference | |
| |
| |
表示由显示的基本偏好UI积木PreferenceFragmentCompat在一个形式RecyclerView 。 此课程提供View数据显示在列表中,并与SharedPreferences关联以存储/检索偏好数据。
在XML中指定偏好层次结构时,每个元素可以指向 Preference的子类,类似于视图层次结构和布局。
这个类包含一个key ,它将被用作SharedPreferences 。 由子类决定如何存储该值。
有关使用首选项构建设置UI的信息,请阅读 Settings指南。
Nested classes |
|
|---|---|
class |
Preference.BaseSavedState 用于管理 |
interface |
Preference.OnPreferenceChangeListener 当此用户更改了此 |
interface |
Preference.OnPreferenceClickListener 单击 |
常量(Constants) |
|
|---|---|
int |
DEFAULT_ORDER 如果不需要特定订单,请指定 |
Public constructors |
|
|---|---|
Preference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) 从XML执行通货膨胀并应用特定于类的基本样式。 |
|
Preference(Context context, AttributeSet attrs, int defStyleAttr) 从XML执行通货膨胀并应用特定于类的基本样式。 |
|
Preference(Context context, AttributeSet attrs) 从XML扩展偏好时调用的构造函数。 |
|
Preference(Context context) 构造函数创建首选项。 |
|
公共方法(Public methods) |
|
|---|---|
boolean |
callChangeListener(Object newValue) 在用户更改首选项之后但在内部状态设置之前调用此方法。 |
int |
compareTo(Preference another) 根据订单(如果设置)比较偏好对象,否则按标题按字母顺序排列。 |
Context |
getContext() 返回此首选项的 |
String |
getDependency() 返回此首选项的依赖项的关键。 |
Bundle |
getExtras() 返回与此首选项关联的额外Bundle对象,如果当前不存在Bundle对象,则创建一个新Bundle。 |
String |
getFragment() 返回与此首选项关联的片段类名称。 |
Drawable |
getIcon() 返回此首选项的图标。 |
Intent |
getIntent() 返回与此首选项关联的 |
String |
getKey() 获取此首选项的关键字,这也是用于将值存储到SharedPreferences中的关键。 |
final int |
getLayoutResource() 获取此首选项将显示为 |
Preference.OnPreferenceChangeListener |
getOnPreferenceChangeListener() 当用户更改此首选项时(但内部状态已更新之前),返回要调用的回调。 |
Preference.OnPreferenceClickListener |
getOnPreferenceClickListener() 返回单击此首选项时要调用的回调。 |
int |
getOrder() 获取此Preference相对于同一级别上的其他Preference对象的顺序。 |
PreferenceManager |
getPreferenceManager() 获取管理此首选项对象树的 |
SharedPreferences |
getSharedPreferences() 返回 |
boolean |
getShouldDisableView() 检查此首选项是否应在其操作处于禁用状态时禁用其视图。 |
CharSequence |
getSummary() 返回此首选项的摘要。 |
CharSequence |
getTitle() 返回此首选项的标题。 |
final int |
getWidgetLayoutResource() 获取此首选项的可控小部件的布局资源。 |
boolean |
hasKey() 检查此首选项是否有有效的密钥。 |
boolean |
isEnabled() 检查此首选项是否应在列表中启用。 |
boolean |
isPersistent() 检查此首选项是否持久。 |
boolean |
isSelectable() 检查此首选项是否应该在列表中可选。 |
final boolean |
isVisible() 检查此首选项是否对列表中的用户可见。 |
void |
notifyDependencyChange(boolean disableDependents) 通知任何监听依赖者影响依赖性的更改。 |
void |
onAttached() 当首选项层次已被附加到首选项列表时调用。 |
void |
onBindViewHolder(PreferenceViewHolder holder) 将创建的视图绑定到此首选项的数据。 |
void |
onDependencyChanged(Preference dependency, boolean disableDependent) 当依赖关系发生变化时调用。 |
void |
onDetached() 当首选项层次已从首选项列表中分离时调用。 |
void |
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfoCompat info) 使用有关此首选项的视图的信息初始化 |
void |
onParentChanged(Preference parent, boolean disableChild) 当隐式父依赖性发生变化时调用。 |
Bundle |
peekExtras() 返回与此首选项关联的额外Bundle对象,如果当前不存在返回null。 |
void |
restoreHierarchyState(Bundle container) 从给定的容器中恢复此首选项层次结构的先前保存的状态。 |
void |
saveHierarchyState(Bundle container) 将此首选项层次结构的冻结状态存储到给定的容器中。 |
void |
setDefaultValue(Object defaultValue) 设置此首选项的默认值,如果持久性处于关闭状态或持久性处于打开状态并且在持久性存储器中未找到首选项,则会设置该首选项。 |
void |
setDependency(String dependencyKey) 设置此首选项将依赖的首选项的关键。 |
void |
setEnabled(boolean enabled) 设置是否启用此首选项。 |
void |
setFragment(String fragment) 设置单击此首选项时要显示的片段的类名称。 |
void |
setIcon(Drawable icon) 使用Drawable设置此首选项的图标。 |
void |
setIcon(int iconResId) 使用资源ID设置此首选项的图标。 |
void |
setIntent(Intent intent) 套 |
void |
setKey(String key) 为此首选项设置密钥,该密钥用作 |
void |
setLayoutResource(int layoutResId) 将此首选项设置为显示为 |
void |
setOnPreferenceChangeListener(Preference.OnPreferenceChangeListener onPreferenceChangeListener) 当用户更改此首选项时(但在内部状态更新之前)设置要调用的回调。 |
void |
setOnPreferenceClickListener(Preference.OnPreferenceClickListener onPreferenceClickListener) 设置单击此首选项时要调用的回调。 |
void |
setOrder(int order) 设置此首选项相对于同一级别上的其他首选项对象的顺序。 |
void |
setPersistent(boolean persistent) 设置此首选项是否持久。 |
void |
setSelectable(boolean selectable) 设置此首选项是否可选。 |
void |
setShouldDisableView(boolean shouldDisableView) 设置此首选项是否应禁用其视图。 |
void |
setSummary(CharSequence summary) 使用CharSequence设置此首选项的摘要。 |
void |
setSummary(int summaryResId) 使用资源ID设置此首选项的摘要。 |
void |
setTitle(CharSequence title) 使用CharSequence设置此首选项的标题。 |
void |
setTitle(int titleResId) 使用资源ID设置此首选项的标题。 |
void |
setViewId(int viewId) 设置绑定后将分配给代表此首选项的整体视图的标识。 |
final void |
setVisible(boolean visible) 设置此首选项是否应在列表中可见。 |
void |
setWidgetLayoutResource(int widgetLayoutResId) 设置此首选项的可控件部分的布局。 |
boolean |
shouldDisableDependents() 检查当前是否应该禁用此首选项的依赖项。 |
String |
toString() 返回对象的字符串表示形式。 |
Protected methods |
|
|---|---|
Preference |
findPreferenceInHierarchy(String key) 使用给定的键在此层次结构中找到首选项(整个项目,甚至高于/低于 |
boolean |
getPersistedBoolean(boolean defaultReturnValue) 尝试从 |
float |
getPersistedFloat(float defaultReturnValue) 尝试从 |
int |
getPersistedInt(int defaultReturnValue) 尝试从 |
long |
getPersistedLong(long defaultReturnValue) 尝试从 |
String |
getPersistedString(String defaultReturnValue) 尝试从 |
void |
notifyChanged() 当这个 |
void |
notifyHierarchyChanged() 应该在从此组添加/移除偏好时调用,或者应该重新评估顺序。 |
void |
onAttachedToHierarchy(PreferenceManager preferenceManager) 当此首选项已附加到首选项层次结构时调用。 |
void |
onClick() 处理点击偏好。 |
Object |
onGetDefaultValue(TypedArray a, int index) 当首选项被夸大并且需要读取默认值属性时调用。 |
void |
onPrepareForRemoval() 当此首选项从层次结构中删除时调用。 |
void |
onRestoreInstanceState(Parcelable state) Hook允许偏好重新应用先前由 |
Parcelable |
onSaveInstanceState() 钩子允许首选项生成其内部状态的表示,稍后可用于创建具有相同状态的新实例。 |
void |
onSetInitialValue(boolean restorePersistedValue, Object defaultValue) 执行此操作来设置首选项的初始值。 |
boolean |
persistBoolean(boolean value) 尝试将布尔值保存到 |
boolean |
persistFloat(float value) 尝试持续浮动到 |
boolean |
persistInt(int value) 尝试持续int到 |
boolean |
persistLong(long value) 尝试持续很长的时间到 |
boolean |
persistString(String value) 尝试将字符串保存到 |
boolean |
shouldPersist() 检查在给定时间调用此方法时,此首选项是否应将值存储/恢复到 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
java.lang.Comparable
|
|
Preference (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
从XML执行通货膨胀并应用特定于类的基本样式。 Preference的构造函数允许子类在充气时使用它们自己的基础样式。 例如, CheckBoxPreference构造函数调用此版本的超类构造函数,并为android.R.attr.checkBoxPreferenceStyle提供android.R.attr.checkBoxPreferenceStyle 。 这允许主题的复选框首选项样式修改所有的基本首选项属性以及CheckBoxPreference类的属性。
| 参数(Parameters) | |
|---|---|
context |
Context: The Context this is associated with, through which it can access the current theme, resources, SharedPreferences, etc. |
attrs |
AttributeSet: The attributes of the XML tag that is inflating the preference. |
defStyleAttr |
int: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults. |
defStyleRes |
int: A resource identifier of a style resource that supplies default values for the view, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults. |
Preference (Context context, AttributeSet attrs, int defStyleAttr)
从XML执行通货膨胀并应用特定于类的基本样式。 Preference的构造函数允许子类在充气时使用它们自己的基础样式。 例如, CheckBoxPreference构造函数调用此版本的超类构造函数,并为android.R.attr.checkBoxPreferenceStyle提供android.R.attr.checkBoxPreferenceStyle 。 这允许主题的复选框首选项样式修改所有基本首选项属性以及CheckBoxPreference类的属性。
| 参数(Parameters) | |
|---|---|
context |
Context: The Context this is associated with, through which it can access the current theme, resources, SharedPreferences, etc. |
attrs |
AttributeSet: The attributes of the XML tag that is inflating the preference. |
defStyleAttr |
int: An attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults. |
Preference (Context context, AttributeSet attrs)
从XML扩展偏好时调用的构造函数。 这是在从XML文件构造首选项时调用的,并提供在XML文件中指定的属性。 此版本使用默认样式0,所以应用的唯一属性值是Context's Theme和给定AttributeSet中的值。
| 参数(Parameters) | |
|---|---|
context |
Context: The Context this is associated with, through which it can access the current theme, resources, SharedPreferences, etc. |
attrs |
AttributeSet: The attributes of the XML tag that is inflating the preference. |
Preference (Context context)
构造函数创建首选项。
| 参数(Parameters) | |
|---|---|
context |
Context: The Context in which to store Preference values. |
boolean callChangeListener (Object newValue)
在用户更改首选项之后但在内部状态设置之前调用此方法。 这允许客户端忽略用户值。
| 参数(Parameters) | |
|---|---|
newValue |
Object: The new value of this Preference. |
| 返回(Returns) | |
|---|---|
boolean |
True if the user value should be set as the preference value (and persisted). |
int compareTo (Preference another)
根据订单(如果设置)比较偏好对象,否则按标题按字母顺序排列。
| 参数(Parameters) | |
|---|---|
another |
Preference: The Preference to compare to this one. |
| 返回(Returns) | |
|---|---|
int |
0 if the same; less than 0 if this Preference sorts ahead of another; greater than 0 if this Preference sorts after another. |
Context getContext ()
返回此首选项的Context 。 偏好层次结构中的每个偏好可以来自不同的上下文(例如,如果多个活动将偏好提供到单个PreferenceFragmentCompat )。 此上下文将用于保存偏好设置值。
| 返回(Returns) | |
|---|---|
Context |
The Context of this Preference. |
String getDependency ()
返回此首选项的依赖项的关键。
| 返回(Returns) | |
|---|---|
String |
The key of the dependency. |
也可以看看:
Bundle getExtras ()
返回与此首选项关联的额外Bundle对象,如果当前不存在Bundle对象,则创建一个新Bundle。 您可以使用它来获取和设置单独的额外键/值对。
| 返回(Returns) | |
|---|---|
Bundle |
|
String getFragment ()
返回与此首选项关联的片段类名称。
| 返回(Returns) | |
|---|---|
String |
The fragment class name last set via setFragment(String) or XML. |
Intent getIntent ()
返回与此偏好相关的 Intent 。
| 返回(Returns) | |
|---|---|
Intent |
The Intent last set via setIntent(Intent) or XML. |
int getLayoutResource ()
获取此首选项将显示为 View的布局资源。
| 返回(Returns) | |
|---|---|
int |
The layout resource ID. |
Preference.OnPreferenceChangeListener getOnPreferenceChangeListener ()
当用户更改此首选项时(但内部状态已更新之前),返回要调用的回调。
| 返回(Returns) | |
|---|---|
Preference.OnPreferenceChangeListener |
The callback to be invoked. |
Preference.OnPreferenceClickListener getOnPreferenceClickListener ()
返回单击此首选项时要调用的回调。
| 返回(Returns) | |
|---|---|
Preference.OnPreferenceClickListener |
The callback to be invoked. |
int getOrder ()
获取此Preference相对于同一级别上的其他Preference对象的顺序。
| 返回(Returns) | |
|---|---|
int |
The order of this Preference. |
也可以看看:
PreferenceManager getPreferenceManager ()
获取管理此首选项对象树的 PreferenceManager 。
| 返回(Returns) | |
|---|---|
PreferenceManager |
The PreferenceManager. |
SharedPreferences getSharedPreferences ()
返回此首选项可以读取其值的SharedPreferences 。 通常情况下,它更容易使用的辅助阅读方法之一: getPersistedBoolean(boolean) , getPersistedFloat(float) , getPersistedInt(int) , getPersistedLong(long) , getPersistedString(String) 。
| 返回(Returns) | |
|---|---|
SharedPreferences |
The SharedPreferences where this Preference reads its value(s), or null if it isn't attached to a Preference hierarchy. |
boolean getShouldDisableView ()
检查此首选项是否应在其操作处于禁用状态时禁用其视图。
| 返回(Returns) | |
|---|---|
boolean |
True if it should disable the view. |
int getWidgetLayoutResource ()
获取此首选项的可控小部件的布局资源。
| 返回(Returns) | |
|---|---|
int |
The layout resource ID. |
boolean hasKey ()
检查此首选项是否有有效的密钥。
| 返回(Returns) | |
|---|---|
boolean |
True if the key exists and is not a blank string, false otherwise. |
boolean isEnabled ()
检查此首选项是否应在列表中启用。
| 返回(Returns) | |
|---|---|
boolean |
True if this Preference is enabled, false otherwise. |
boolean isPersistent ()
检查此首选项是否持久。 如果是,则将其值存储到持久性SharedPreferences存储中。
| 返回(Returns) | |
|---|---|
boolean |
True if it is persistent. |
boolean isSelectable ()
检查此首选项是否应该在列表中可选。
| 返回(Returns) | |
|---|---|
boolean |
True if it is selectable, false otherwise. |
boolean isVisible ()
检查此首选项是否对列表中的用户可见。
| 返回(Returns) | |
|---|---|
boolean |
True if this preference should be displayed. |
也可以看看:
void notifyDependencyChange (boolean disableDependents)
通知任何监听依赖者影响依赖性的更改。
| 参数(Parameters) | |
|---|---|
disableDependents |
boolean: Whether this Preference should disable its dependents. |
void onAttached ()
当首选项层次已被附加到首选项列表时调用。 当此首选项已附加到已连接到首选项列表的组时,也可以调用此选项。
void onBindViewHolder (PreferenceViewHolder holder)
将创建的视图绑定到此首选项的数据。
这是抓取对布局和自定义属性的自定义视图的引用的好地方。
确保调用超类的实现。
| 参数(Parameters) | |
|---|---|
holder |
PreferenceViewHolder: The ViewHolder that provides references to the views to fill in. These views will be recycled, so you should not hold a reference to them after this method returns. |
void onDependencyChanged (Preference dependency, boolean disableDependent)
当依赖关系发生变化时调用。
| 参数(Parameters) | |
|---|---|
dependency |
Preference: The Preference that this Preference depends on. |
disableDependent |
boolean: Set true to disable this Preference. |
void onDetached ()
当首选项层次已从首选项列表中分离时调用。 当此首选项已从附加到首选项列表的组中移除时,也可以调用此选项。
void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfoCompat info)
使用有关此首选项的视图的信息初始化 AccessibilityNodeInfo 。
| 参数(Parameters) | |
|---|---|
info |
AccessibilityNodeInfoCompat
|
void onParentChanged (Preference parent, boolean disableChild)
当隐式父依赖性发生变化时调用。
| 参数(Parameters) | |
|---|---|
parent |
Preference: The Preference that this Preference depends on. |
disableChild |
boolean: Set true to disable this Preference. |
void restoreHierarchyState (Bundle container)
从给定的容器中恢复此首选项层次结构的先前保存的状态。
| 参数(Parameters) | |
|---|---|
container |
Bundle: The Bundle that holds the previously saved state. |
void saveHierarchyState (Bundle container)
将此首选项层次结构的冻结状态存储到给定的容器中。
| 参数(Parameters) | |
|---|---|
container |
Bundle: The Bundle in which to save the instance of this Preference. |
void setDefaultValue (Object defaultValue)
设置此首选项的默认值,如果持久性处于关闭状态或持久性处于打开状态并且在持久性存储器中未找到首选项,则会设置该首选项。
| 参数(Parameters) | |
|---|---|
defaultValue |
Object: The default value. |
void setDependency (String dependencyKey)
设置此首选项将依赖的首选项的关键。 如果该首选项未设置或关闭,则此首选项将被禁用。
| 参数(Parameters) | |
|---|---|
dependencyKey |
String: The key of the Preference that this depends on. |
void setEnabled (boolean enabled)
设置是否启用此首选项。 如果禁用,则不会处理点击。
| 参数(Parameters) | |
|---|---|
enabled |
boolean: Set true to enable it. |
void setFragment (String fragment)
设置单击此首选项时要显示的片段的类名称。
| 参数(Parameters) | |
|---|---|
fragment |
String: The class name of the fragment associated with this Preference. |
void setIcon (Drawable icon)
使用Drawable设置此首选项的图标。 该图标将被置于由onBindViewHolder(PreferenceViewHolder)创建的视图中的ID icon中。
| 参数(Parameters) | |
|---|---|
icon |
Drawable: The optional icon for this Preference. |
void setIcon (int iconResId)
使用资源ID设置此首选项的图标。
| 参数(Parameters) | |
|---|---|
iconResId |
int: The icon as a resource ID. |
也可以看看:
void setIntent (Intent intent)
当此首选项被点击时,将 Intent设置为 startActivity(Intent) 。
| 参数(Parameters) | |
|---|---|
intent |
Intent: The intent associated with this Preference. |
void setKey (String key)
设置此首选项的密钥,该密钥用作SharedPreferences的密钥。 这对于包装应该是独一无二的。
| 参数(Parameters) | |
|---|---|
key |
String: The key for the preference. |
void setLayoutResource (int layoutResId)
设置为此偏好显示的View充气布局资源。 在大多数情况下,默认布局对于自定义首选项对象是足够的,只有小部件布局需要更改。
这种布局应该包含ViewGroup ID为widget_frame成为这种偏好的特定部件的父。 它应该类似地包含title和summary 。
将首选项添加到 PreferenceGroup之后更改布局是错误的
| 参数(Parameters) | |
|---|---|
layoutResId |
int: The layout resource ID to be inflated and returned as a View. |
也可以看看:
void setOnPreferenceChangeListener (Preference.OnPreferenceChangeListener onPreferenceChangeListener)
当用户更改此首选项时(但在内部状态更新之前)设置要调用的回调。
| 参数(Parameters) | |
|---|---|
onPreferenceChangeListener |
Preference.OnPreferenceChangeListener: The callback to be invoked. |
void setOnPreferenceClickListener (Preference.OnPreferenceClickListener onPreferenceClickListener)
设置单击此首选项时要调用的回调。
| 参数(Parameters) | |
|---|---|
onPreferenceClickListener |
Preference.OnPreferenceClickListener: The callback to be invoked. |
void setOrder (int order)
设置此首选项相对于同一级别上的其他首选项对象的顺序。 如果没有指定,默认行为是按字母顺序排序。 setOrderingAsAdded(boolean)可用于根据它们在XML中出现的顺序对Preference对象进行排序。
| 参数(Parameters) | |
|---|---|
order |
int: The order for this Preference. A lower value will be shown first. Use DEFAULT_ORDER to sort alphabetically or allow ordering from XML. |
void setPersistent (boolean persistent)
设置此首选项是否持久。 当持续的,它存储的值(S)为持久SharedPreferences存储。
| 参数(Parameters) | |
|---|---|
persistent |
boolean: Set true if it should store its value(s) into the SharedPreferences. |
void setSelectable (boolean selectable)
设置此首选项是否可选。
| 参数(Parameters) | |
|---|---|
selectable |
boolean: Set true to make it selectable. |
void setShouldDisableView (boolean shouldDisableView)
设置此首选项是否应禁用其视图。
例如,将此设为 setEnabled(boolean)为仅显示信息的首选项为false,并且1)不应该可点击2)不应将视图设置为禁用状态。
| 参数(Parameters) | |
|---|---|
shouldDisableView |
boolean: Set true if this preference should disable its view when the preference is disabled. |
void setSummary (CharSequence summary)
使用CharSequence设置此首选项的摘要。
| 参数(Parameters) | |
|---|---|
summary |
CharSequence: The summary for the preference. |
void setSummary (int summaryResId)
使用资源ID设置此首选项的摘要。
| 参数(Parameters) | |
|---|---|
summaryResId |
int: The summary as a resource. |
也可以看看:
void setTitle (CharSequence title)
使用CharSequence设置此首选项的标题。 该标题将被置于title范围内的视图onBindViewHolder(PreferenceViewHolder) 。
| 参数(Parameters) | |
|---|---|
title |
CharSequence: The title for this Preference. |
void setTitle (int titleResId)
使用资源ID设置此首选项的标题。
| 参数(Parameters) | |
|---|---|
titleResId |
int: The title as a resource ID. |
也可以看看:
void setViewId (int viewId)
设置绑定后将分配给代表此首选项的整体视图的标识。
| 参数(Parameters) | |
|---|---|
viewId |
int
|
也可以看看:
void setVisible (boolean visible)
设置此首选项是否应在列表中可见。 如果为false,则将其从适配器中排除,但仍可以使用findPreference(CharSequence)进行检索。
| 参数(Parameters) | |
|---|---|
visible |
boolean: Set false if this preference should be hidden from the list. |
void setWidgetLayoutResource (int widgetLayoutResId)
设置此首选项的可控件部分的布局。 这是膨胀到主布局。 例如, CheckBoxPreference会在这里指定自定义布局(仅由CheckBox组成),而不是创建自己的主布局。
将偏好添加到 PreferenceGroup后更改布局是错误的
| 参数(Parameters) | |
|---|---|
widgetLayoutResId |
int: The layout resource ID to be inflated into the main layout. |
也可以看看:
boolean shouldDisableDependents ()
检查当前是否应该禁用此首选项的依赖项。
| 返回(Returns) | |
|---|---|
boolean |
True if the dependents should be disabled, otherwise false. |
String toString ()
返回对象的字符串表示形式。 通常, toString方法返回一个“文本表示”该对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。
类Object的toString方法返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @ ”以及对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:
getClass().getName() + '@' + Integer.toHexString(hashCode())
| 返回(Returns) | |
|---|---|
String |
a string representation of the object. |
Preference findPreferenceInHierarchy (String key)
用这个给定的键在这个层次结构中找到一个偏好(整个事情,甚至高于/低于你的 PreferenceScreen屏幕截图)。
这只在我们被附加到层次结构后才起作用。
| 参数(Parameters) | |
|---|---|
key |
String: The key of the Preference to find. |
| 返回(Returns) | |
|---|---|
Preference |
The Preference that uses the given key. |
boolean getPersistedBoolean (boolean defaultReturnValue)
尝试从 SharedPreferences获取持久布尔值。
| 参数(Parameters) | |
|---|---|
defaultReturnValue |
boolean: The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences. |
| 返回(Returns) | |
|---|---|
boolean |
The value from the SharedPreferences or the default return value. |
float getPersistedFloat (float defaultReturnValue)
尝试从 SharedPreferences获得持续浮动。
| 参数(Parameters) | |
|---|---|
defaultReturnValue |
float: The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences. |
| 返回(Returns) | |
|---|---|
float |
The value from the SharedPreferences or the default return value. |
int getPersistedInt (int defaultReturnValue)
尝试从 SharedPreferences获取持久化int。
| 参数(Parameters) | |
|---|---|
defaultReturnValue |
int: The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences. |
| 返回(Returns) | |
|---|---|
int |
The value from the SharedPreferences or the default return value. |
long getPersistedLong (long defaultReturnValue)
尝试从 SharedPreferences获得长时间持续。
| 参数(Parameters) | |
|---|---|
defaultReturnValue |
long: The default value to return if either this Preference is not persistent or this Preference is not in the SharedPreferences. |
| 返回(Returns) | |
|---|---|
long |
The value from the SharedPreferences or the default return value. |
String getPersistedString (String defaultReturnValue)
尝试从 SharedPreferences获取持久字符串。
这将检查此首选项是否持久,从 PreferenceManager获取SharedPreferences,并获取该值。
| 参数(Parameters) | |
|---|---|
defaultReturnValue |
String: The default value to return if either the Preference is not persistent or the Preference is not in the shared preferences. |
| 返回(Returns) | |
|---|---|
String |
The value from the SharedPreferences or the default return value. |
也可以看看:
void notifyHierarchyChanged ()
应该在从此组添加/移除偏好时调用,或者应该重新评估顺序。
void onAttachedToHierarchy (PreferenceManager preferenceManager)
当此首选项已附加到首选项层次结构时调用。 确保调用超级实现。
| 参数(Parameters) | |
|---|---|
preferenceManager |
PreferenceManager: The PreferenceManager of the hierarchy. |
void onClick ()
处理点击偏好。 这包括将值保存到SharedPreferences 。 但是,重写的方法应该调用callChangeListener(Object)以确保客户端想要用新值更新首选项的状态。
Object onGetDefaultValue (TypedArray a, int index)
当首选项被夸大并且需要读取默认值属性时调用。 由于不同的首选项类型具有不同的值类型,因此子类应获取并返回默认值,该默认值将是其值类型。
例如,如果值类型是字符串,则该方法的主体将代理到 getString(int) 。
| 参数(Parameters) | |
|---|---|
a |
TypedArray: The set of attributes. |
index |
int: The index of the default value attribute. |
| 返回(Returns) | |
|---|---|
Object |
The default value of this preference type. |
void onPrepareForRemoval ()
当此首选项从层次结构中删除时调用。 您应该删除对此知识的任何引用。 确保通过超类实现进行调用。
void onRestoreInstanceState (Parcelable state)
允许偏好的钩子重新应用先前由onSaveInstanceState()生成的其内部状态的表示。 这个函数永远不会被调用为null状态。
| 参数(Parameters) | |
|---|---|
state |
Parcelable: The saved state that had previously been returned by onSaveInstanceState(). |
Parcelable onSaveInstanceState ()
钩子允许首选项生成其内部状态的表示,稍后可用于创建具有相同状态的新实例。 这个状态应该只包含不持久的或者稍后可以重建的信息。
| 返回(Returns) | |
|---|---|
Parcelable |
A Parcelable object containing the current dynamic state of this Preference, or null if there is nothing interesting to save. The default implementation returns null. |
void onSetInitialValue (boolean restorePersistedValue,
Object defaultValue)
执行此操作来设置首选项的初始值。
如果restorePersistedValue为true,则应从SharedPreferences恢复首选项值。 如果restorePersistedValue为false,则应将Preference值设置为给定的defaultValue(并且如果shouldPersist()为true,则可能存储到SharedPreferences)。
这可能并不总是被称为。 一个例子是,如果它不应该坚持,但没有给出默认值。
| 参数(Parameters) | |
|---|---|
restorePersistedValue |
boolean: True to restore the persisted value; false to use the given defaultValue. |
defaultValue |
Object: The default value for this Preference. Only use this if restorePersistedValue is false. |
boolean persistBoolean (boolean value)
尝试将布尔值保存到 SharedPreferences 。
| 参数(Parameters) | |
|---|---|
value |
boolean: The value to persist. |
| 返回(Returns) | |
|---|---|
boolean |
True if this Preference is persistent. (This is not whether the value was persisted, since we may not necessarily commit if there will be a batch commit later.) |
boolean persistFloat (float value)
尝试持续浮动到 SharedPreferences 。
| 参数(Parameters) | |
|---|---|
value |
float: The value to persist. |
| 返回(Returns) | |
|---|---|
boolean |
True if this Preference is persistent. (This is not whether the value was persisted, since we may not necessarily commit if there will be a batch commit later.) |
boolean persistInt (int value)
尝试持续int到 SharedPreferences 。
| 参数(Parameters) | |
|---|---|
value |
int: The value to persist. |
| 返回(Returns) | |
|---|---|
boolean |
True if the Preference is persistent. (This is not whether the value was persisted, since we may not necessarily commit if there will be a batch commit later.) |
boolean persistLong (long value)
尝试持续很长的时间到 SharedPreferences 。
| 参数(Parameters) | |
|---|---|
value |
long: The value to persist. |
| 返回(Returns) | |
|---|---|
boolean |
True if this Preference is persistent. (This is not whether the value was persisted, since we may not necessarily commit if there will be a batch commit later.) |
boolean persistString (String value)
尝试将字符串保存到 SharedPreferences 。
这将检查这个首选项是否持久,从 PreferenceManager获得编辑器,放入字符串,并检查是否应该提交(如果是,则提交)。
| 参数(Parameters) | |
|---|---|
value |
String: The value to persist. |
| 返回(Returns) | |
|---|---|
boolean |
True if the Preference is persistent. (This is not whether the value was persisted, since we may not necessarily commit if there will be a batch commit later.) |
也可以看看:
boolean shouldPersist ()
检查在给定时间调用此方法时,此首选项是否应将值存储/恢复到SharedPreferences 。 这至少会检查此首选项是否持久,并且它目前有一个密钥。 在从SharedPreferences保存/恢复之前,请先检查这一点。
| 返回(Returns) | |
|---|---|
boolean |
True if it should persist the value. |