android 2.2 apidemos 赏析笔记 5
16lz
2022-07-28
PreferencesFromXml.java
SEE:
1.
addPreferencesFromResource(R.xml.preferences);
2.R.xml.preferences.XML
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/inline_preferences">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/title_toggle_preference"
android:summary="@string/summary_toggle_preference" />
</PreferenceCategory>
<EditTextPreference
android:dialogTitle="@string/dialog_title_edittext_preference"
<ListPreference
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
<PreferenceScreen
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.android.com" />
</PreferenceScreen>
<CheckBoxPreference
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
SO:
具体的设置方法看代码
PreferenceDependencies.java
SEE:
1.R.xml.preference_dependencies
android:dependency="wifi"
SO:
完。。
DefaultValues.java
SEE:R.xml.default_values
1.
<CheckBoxPreference
android:defaultValue="true"
2.
<EditTextPreference
android:defaultValue="@string/default_value_edittext_preference"
3.
<ListPreference
android:defaultValue="@string/default_value_list_preference"
SO:
不同的Value设置。完。。。。
PreferencesFromCode
SEE:
1.
public class PreferencesFromCode extends PreferenceActivity {
2.
PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
inlinePrefCat.setTitle(R.string.inline_preferences);
root.addPreference(inlinePrefCat);
3.
TypedArray a = obtainStyledAttributes(R.styleable.TogglePrefAttrs);
childCheckBoxPref.setLayoutResource(
a.getResourceId(R.styleable.TogglePrefAttrs_android_preferenceLayoutChild,
0));
a.recycle();
4.
inlinePrefCat.setTitle(R.string.inline_preferences);
5.
SO:
具体设置方法见代码。这里没有设置dependency 我设置了 parentCheckBoxPref.setKey("key1");
childCheckBoxPref.setDependency(parentCheckBoxPref.getKey());
都不行,奇怪。难道不可以从代码写,还是用XML好了。
AdvancedPreferences.java
SEE:
PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);
SO:
从XML中设置默认值,而不是布局。。。RI
SEE:
1.
public class AdvancedPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
2.AdvancedPreferences.java
mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
KEY_ADVANCED_CHECKBOX_PREFERENCE);
SO:
findViewById的PreferenceActivity版。。
SEE:
1.
mHandler.postDelayed(this, 1000);
2.
mForceCheckBoxRunnable.run();
3.
mHandler.removeCallbacks(mForceCheckBoxRunnable);
SO:
设置线程与运行。mHandler是控制的主线程,一秒后继续再次运行该Runnable,使用mHandler来取消。
SEE:
1.
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
2.
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
SO:
设置Preference监听与取消监听
SEE:
1.preference_widget_mypreference.xml
<TextView
android:id="@+id/mypreference_widget"
android:focusable="false"
android:clickable="false"
2.MyPreference.JAVA
setWidgetLayoutResource(R.layout.preference_widget_mypreference);
3.MyPreference.JAVA
final TextView myTextView = (TextView) view.findViewById(R.id.mypreference_widget);
if (myTextView != null) {
myTextView.setText(String.valueOf(mClickCounter));
}
SO:
神奇的设置,这里的TextView是用来显示数字的那个,不是整个preference的布局
SEE: MyPreference.JAVA 自定义preference的方法 MARK
1.
!callChangeListener(newValue)
2.
persistInt(mClickCounter);
3.
notifyChanged();
4.
protected Object onGetDefaultValue(TypedArray a, int index) {
// This preference type's value type is Integer, so we read the default
// value from the attributes as an Integer.
return a.getInteger(index, 0);
}
5.
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
6.
protected Parcelable onSaveInstanceState() {
7.
protected void onRestoreInstanceState(Parcelable state) {
8.
private static class SavedState extends BaseSavedState {
clickCounter = source.readInt();
dest.writeInt(clickCounter);
SO:
1.如果是invalid就不能改变。
2.保存值,
3.通知改变,观察者模式。
4.返回默认值
5.设置值的方法
6.有点扯不来了,这个是被临时破坏的时候,通过这个方法保存 同activity的那个。
7.回复值的时候调用
8.扯不来了,自己理解吧。。仿照下就好。。继承了该数据结构,外加一个附带的值就好。。
然后弄清楚读取和保存的地方就好。。
SEE:
1.
addPreferencesFromResource(R.xml.preferences);
2.R.xml.preferences.XML
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/inline_preferences">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/title_toggle_preference"
android:summary="@string/summary_toggle_preference" />
</PreferenceCategory>
<EditTextPreference
android:dialogTitle="@string/dialog_title_edittext_preference"
<ListPreference
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
<PreferenceScreen
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.android.com" />
</PreferenceScreen>
<CheckBoxPreference
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
SO:
具体的设置方法看代码
PreferenceDependencies.java
SEE:
1.R.xml.preference_dependencies
android:dependency="wifi"
SO:
完。。
DefaultValues.java
SEE:R.xml.default_values
1.
<CheckBoxPreference
android:defaultValue="true"
2.
<EditTextPreference
android:defaultValue="@string/default_value_edittext_preference"
3.
<ListPreference
android:defaultValue="@string/default_value_list_preference"
SO:
不同的Value设置。完。。。。
PreferencesFromCode
SEE:
1.
public class PreferencesFromCode extends PreferenceActivity {
2.
PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
inlinePrefCat.setTitle(R.string.inline_preferences);
root.addPreference(inlinePrefCat);
3.
TypedArray a = obtainStyledAttributes(R.styleable.TogglePrefAttrs);
childCheckBoxPref.setLayoutResource(
a.getResourceId(R.styleable.TogglePrefAttrs_android_preferenceLayoutChild,
0));
a.recycle();
4.
inlinePrefCat.setTitle(R.string.inline_preferences);
5.
SO:
具体设置方法见代码。这里没有设置dependency 我设置了 parentCheckBoxPref.setKey("key1");
childCheckBoxPref.setDependency(parentCheckBoxPref.getKey());
都不行,奇怪。难道不可以从代码写,还是用XML好了。
AdvancedPreferences.java
SEE:
PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);
SO:
从XML中设置默认值,而不是布局。。。RI
SEE:
1.
public class AdvancedPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
2.AdvancedPreferences.java
mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
KEY_ADVANCED_CHECKBOX_PREFERENCE);
SO:
findViewById的PreferenceActivity版。。
SEE:
1.
mHandler.postDelayed(this, 1000);
2.
mForceCheckBoxRunnable.run();
3.
mHandler.removeCallbacks(mForceCheckBoxRunnable);
SO:
设置线程与运行。mHandler是控制的主线程,一秒后继续再次运行该Runnable,使用mHandler来取消。
SEE:
1.
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
2.
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
SO:
设置Preference监听与取消监听
SEE:
1.preference_widget_mypreference.xml
<TextView
android:id="@+id/mypreference_widget"
android:focusable="false"
android:clickable="false"
2.MyPreference.JAVA
setWidgetLayoutResource(R.layout.preference_widget_mypreference);
3.MyPreference.JAVA
final TextView myTextView = (TextView) view.findViewById(R.id.mypreference_widget);
if (myTextView != null) {
myTextView.setText(String.valueOf(mClickCounter));
}
SO:
神奇的设置,这里的TextView是用来显示数字的那个,不是整个preference的布局
SEE: MyPreference.JAVA 自定义preference的方法 MARK
1.
!callChangeListener(newValue)
2.
persistInt(mClickCounter);
3.
notifyChanged();
4.
protected Object onGetDefaultValue(TypedArray a, int index) {
// This preference type's value type is Integer, so we read the default
// value from the attributes as an Integer.
return a.getInteger(index, 0);
}
5.
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
6.
protected Parcelable onSaveInstanceState() {
7.
protected void onRestoreInstanceState(Parcelable state) {
8.
private static class SavedState extends BaseSavedState {
clickCounter = source.readInt();
dest.writeInt(clickCounter);
SO:
1.如果是invalid就不能改变。
2.保存值,
3.通知改变,观察者模式。
4.返回默认值
5.设置值的方法
6.有点扯不来了,这个是被临时破坏的时候,通过这个方法保存 同activity的那个。
7.回复值的时候调用
8.扯不来了,自己理解吧。。仿照下就好。。继承了该数据结构,外加一个附带的值就好。。
然后弄清楚读取和保存的地方就好。。
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- android上一些方法的区别和用法的注意事项
- android EditText设置不可写
- android实现字体闪烁动画的方法
- android“设置”里的版本号
- Android中dispatchDraw分析
- Android四大基本组件介绍与生命周期