Android development Preference usage, androidpreference
1. Preference is a base class provided by androidSDK from API1. It is used to display the interface to users.
2. When Preference is used to display an activity, this activity must inherit the Preferenceactivity and implement addPreferencesFromResource (R. xml. main) load the layout file. The layout file needs to create a new xml folder under the res folder. Create an xml file. 3. There are three labels in the layout file: PreferenceScreen root node PreferenceCategory classification node Preference subnode
<?xml version="1.0" encoding="utf-8"?><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="@string/general" > <Preference android:title="@string/properties" /> <Preference android:title="@string/attach" /> <Preference android:title="@string/left_and_right" /> <Preference android:title="@string/changing_fragments" /> <Preference android:title="@string/responsive_ui" /> <Preference android:title="@string/viewpager" /> </PreferenceCategory> <PreferenceCategory android:title="@string/title_bar" > <Preference android:title="@string/title_bar_slide" /> <Preference android:title="@string/title_bar_content" /> </PreferenceCategory> <PreferenceCategory android:title="@string/animations" > <Preference android:title="@string/anim_zoom" /> <Preference android:title="@string/anim_scale" /> <Preference android:title="@string/anim_slide" /> </PreferenceCategory></PreferenceScreen>
4. Rewrite the onPreferenceTreeClick method to implement listening.
@Override public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference pref) { Class<?> cls = null; String title = pref.getTitle().toString(); if (title.equals(getString(R.string.properties))) { cls = PropertiesActivity.class; } else if (title.equals(getString(R.string.attach))) { cls = AttachExample.class; } else if (title.equals(getString(R.string.changing_fragments))) { cls = FragmentChangeActivity.class; } else if (title.equals(getString(R.string.left_and_right))) { cls = LeftAndRightActivity.class; } else if (title.equals(getString(R.string.responsive_ui))) { cls = ResponsiveUIActivity.class; } else if (title.equals(getString(R.string.viewpager))) { cls = ViewPagerActivity.class; } else if (title.equals(getString(R.string.title_bar_slide))) { cls = SlidingTitleBar.class; } else if (title.equals(getString(R.string.title_bar_content))) { cls = SlidingContent.class; } else if (title.equals(getString(R.string.anim_zoom))) { cls = CustomZoomAnimation.class; } else if (title.equals(getString(R.string.anim_scale))) { cls = CustomScaleAnimation.class; } else if (title.equals(getString(R.string.anim_slide))) { cls = CustomSlideAnimation.class; } Intent intent = new Intent(this, cls); startActivity(intent); return true; }