PreferenceFragment设置界面的编写

效果图:
这里写图片描述

布局文件:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory
        android:title="@string/basic_setting">

        <CheckBoxPreference
            android:defaultValue="false"
            android:key="switch_noPhotoMode"
            android:summary="@string/switch_no_photo_mode_summary"
            android:title="@string/switch_photo_mode"/>

        <Preference
            android:key="auto_nightMode"
            android:summary="@string/auto_night_mode_summary"
            android:title="@string/auto_night_mode"/>

        <PreferenceCategory
            android:title="@string/video_setting">

            <CheckBoxPreference
                android:defaultValue="false"
                android:key="video_force_landscape"
                android:summary="@string/video_force_landscape_summary"
                android:title="@string/video_force_landscape"/>

            <CheckBoxPreference
                android:defaultValue="false"
                android:key="video_auto_play"
                android:summary="@string/video_auto_play_summary"
                android:title="@string/video_auto_play"/>

        </PreferenceCategory>
        <Preference
            android:key="text_size"
            android:summary="设置字体大小"
            android:title="字体大小"/>

    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/other_setting">

        <com.meiji.toutiao.widget.IconListPreference
            android:defaultValue="0"
            android:entries="@array/icons"
            android:entryValues="@array/iconsValues"
            android:key="custom_icon"
            android:summary="@string/choose_icon_summary"
            android:title="@string/choose_icon"
            app:iconsDrawables="@array/iconsDrawables"/>

        <com.meiji.toutiao.widget.IconPreference
            android:key="color"
            android:summary="@string/choose_theme_color_summary"
            android:title="@string/choose_theme_color"/>

        <ListPreference
            android:defaultValue="1"
            android:entries="@array/slidable"
            android:entryValues="@array/slidableValues"
            android:key="slidable"
            android:negativeButtonText="@string/cancel"
            android:summary="@string/slidable_summary"
            android:title="@string/slidable_title"/>

        <CheckBoxPreference
            android:defaultValue="false"
            android:key="nav_bar"
            android:summary="@string/nav_bar_coloration_summary"
            android:title="@string/nav_bar_coloration"/>

        <Preference
            android:key="clearCache"
            android:title="@string/clear_cache"/>

    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/about_settings">

        <Preference
            android:key="version"
            android:title="@string/version"/>

        <Preference
            android:key="changelog"
            android:summary="@string/changelog_url"
            android:title="@string/changelog"/>

        <Preference
            android:key="licenses"
            android:summary="@string/about_libraries_summary"
            android:title="@string/about_libraries_label"/>

        <Preference
            android:key="sourceCode"
            android:summary="@string/source_code_url"
            android:title="@string/source_code"/>

        <Preference
            android:key="copyRight"
            android:summary="@string/copyright_summary"
            android:title="@string/copyright"/>

    </PreferenceCategory>

</PreferenceScreen>

Java代码:

public class GeneralPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

    private IconPreference colorPreview;
    private SettingActivity context;

    public static GeneralPreferenceFragment newInstance() {
        return new GeneralPreferenceFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);
        context = (SettingActivity) getActivity();
        setHasOptionsMenu(true);
        setText();

        findPreference("auto_nightMode").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                context.startWithFragment(AutoNightModeFragment.class.getName(), null, null, 0, null);
                return true;
            }
        });

        findPreference("text_size").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                context.startWithFragment(TextSizeFragment.class.getName(), null, null, 0, null);
                return true;
            }
        });

        findPreference("custom_icon").setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {

                int selectValue = Integer.parseInt((String) newValue);
                int drawable = Constant.ICONS_DRAWABLES[selectValue];

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ActivityManager.TaskDescription tDesc = new ActivityManager.TaskDescription(
                            getString(R.string.app_name),
                            BitmapFactory.decodeResource(getResources(), drawable),
                            SettingUtil.getInstance().getColor());
                    context.setTaskDescription(tDesc);
                }

                return true;
            }
        });

        findPreference("color").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                new ColorChooserDialog.Builder(context, R.string.choose_theme_color)
                        .backButton(R.string.back)
                        .cancelButton(R.string.cancel)
                        .doneButton(R.string.done)
                        .customButton(R.string.custom)
                        .presetsButton(R.string.back)
                        .allowUserColorInputAlpha(false)
                        .show();
                return false;
            }
        });

        colorPreview = (IconPreference) findPreference("color");

        findPreference("nav_bar").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                int color = SettingUtil.getInstance().getColor();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    if (SettingUtil.getInstance().getNavBar()) {
                        context.getWindow().setNavigationBarColor(CircleView.shiftColorDown(CircleView.shiftColorDown(color)));
                    } else {
                        context.getWindow().setNavigationBarColor(Color.BLACK);
                    }
                }
                return false;
            }
        });

        findPreference("clearCache").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                CacheDataManager.clearAllCache(context);
                Snackbar.make(getView(), R.string.clear_cache_successfully, Snackbar.LENGTH_SHORT).show();
                setText();
                return false;
            }
        });

        try {
            String version = "当前版本 " + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
            findPreference("version").setSummary(version);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        findPreference("changelog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.changelog_url))));
                return false;
            }
        });

        findPreference("licenses").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                createLicenseDialog();
                return false;
            }
        });

        findPreference("sourceCode").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.source_code_url))));
                return false;
            }
        });

        findPreference("copyRight").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                new AlertDialog.Builder(context)
                        .setTitle(R.string.copyright)
                        .setMessage(R.string.copyright_content)
                        .setCancelable(true)
                        .show();
                return false;
            }
        });
    }

    private void setText() {
        try {
            findPreference("clearCache").setSummary(CacheDataManager.getTotalCacheSize(context));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void createLicenseDialog() {
        Notices notices = new Notices();
        notices.addNotice(new Notice("PhotoView", "https://github.com/chrisbanes/PhotoView", "Copyright 2017 Chris Banes", new ApacheSoftwareLicense20()));
        notices.addNotice(new Notice("OkHttp", "https://github.com/square/okhttp", "Copyright 2016 Square, Inc.", new ApacheSoftwareLicense20()));
        notices.addNotice(new Notice("Gson", "https://github.com/google/gson", "Copyright 2008 Google Inc.", new ApacheSoftwareLicense20()));
        notices.addNotice(new Notice("Glide", "https://github.com/bumptech/glide", "Sam Judd - @sjudd on GitHub, @samajudd on Twitter", new ApacheSoftwareLicense20()));
        notices.addNotice(new Notice("Stetho", "https://github.com/facebook/stetho", "Copyright (c) 2015, Facebook, Inc. All rights reserved.", new BSD3ClauseLicense()));
        notices.addNotice(new Notice("PersistentCookieJar", "https://github.com/franmontiel/PersistentCookieJar", "Copyright 2016 Francisco José Montiel Navarro", new ApacheSoftwareLicense20()));
        notices.addNotice(new Notice("jsoup", "https://jsoup.org", "Copyright © 2009 - 2016 Jonathan Hedley ([email protected])", new MITLicense()));

        new LicensesDialog.Builder(context)
                .setNotices(notices)
                .setIncludeOwnLicense(true)
                .build()
                .show();
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("color")) {
            colorPreview.setView();
        }
        if (key.equals("slidable")) {
            context.recreate();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u010838785/article/details/79486441