android开发Preference的使用

1  、Preference是androidSDK提供的一个基类从API1就开始有了,用于显示界面给用户。

2  、在使用Preference显示activity时,此activity需要继承Preferenceactivity,  在oncreate方法里面实现addPreferencesFromResource(R.xml.main)加载布局文件  ,次布局文件需要在res文件夹下 新建一个xml文件夹 。在新建一个xml文件。
3 、布局文件有三种标签
PreferenceScreen  根节点
PreferenceCategory  分类结点
Preference  子节点
<?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 、重写 onPreferenceTreeClick方法实现监听

    @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;
    }

 

 
posted @ 2016-07-26 18:08  lihuiupupup  阅读(279)  评论(0编辑  收藏  举报