preference用法for_notification用法

preference用法for_notification用法PreferenceFragment引入在Android3.0之前,设置界面使用PreferenceActivity,在Android3.0之后,官方推荐使用PreferenceFragment,对应于碎片化技术。使用新建Fragment继承PreferenceFragment,加载选项配置xml文件。publicstaticclassNotificationPreferenceFr…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

PreferenceFragment

在Android3.0之前,设置界面使用PreferenceActivity,在Android3.0之后,官方推荐使用PreferenceFragment,对应于碎片化技术。

使用

新建Fragment

新建GeneralPreferenceFragment 继承PreferenceFragment,加载选项配置xml文件。

public static class GeneralPreferenceFragment extends PreferenceFragment { 
   
        @Override
        public void onCreate(Bundle savedInstanceState) { 
   
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_general);
            setHasOptionsMenu(true);

            // Bind the summaries of EditText/List/Dialog/Ringtone preferences
            // to their values. When their values change, their summaries are
            // updated to reflect the new value, per the Android Design
            // guidelines.
            bindPreferenceSummaryToValue(findPreference("example_text"));
            bindPreferenceSummaryToValue(findPreference("example_list"));
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) { 
   
            int id = item.getItemId();
            if (id == android.R.id.home) { 
   
                startActivity(new Intent(getActivity(), SettingsActivity.class));
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }

新建xml文件

在res文件夹目录下新建xml文件夹,在xml文件夹新建pref_general.xml

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

    <SwitchPreference android:defaultValue="true" android:key="example_switch" android:summary="@string/pref_description_social_recommendations" android:title="@string/pref_title_social_recommendations" />

    <!-- NOTE: EditTextPreference accepts EditText attributes. -->
    <!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
    <EditTextPreference android:capitalize="words" android:defaultValue="@string/pref_default_display_name" android:inputType="textCapWords" android:key="example_text" android:maxLines="1" android:selectAllOnFocus="true" android:singleLine="true" android:title="@string/pref_title_display_name" />

    <!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to dismiss it. -->
    <!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
    <ListPreference android:defaultValue="-1" android:entries="@array/pref_example_list_titles" android:entryValues="@array/pref_example_list_values" android:key="example_list" android:negativeButtonText="@null" android:positiveButtonText="@null" android:title="@string/pref_title_add_friends_to_messages" />

</PreferenceScreen>

  • PreferenceScreen:根标签。
  • SwitchPreference:开关标签
  • EditTextPreference:编辑标签
  • ListPreference:集合标签,多选一。

效果图

在这里插入图片描述

分类

使用PreferenceCategory标签进行分类。

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


    <!-- NOTE: EditTextPreference accepts EditText attributes. -->
    <!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
    <PreferenceCategory android:key="@string/pref_header_mqtt" android:summary="@string/pref_header_mqtt" android:title="@string/pref_header_mqtt">
        <EditTextPreference android:capitalize="characters" android:defaultValue="@string/pref_defult_mqtt_ip" android:inputType="textCapWords" android:key="@string/pref_key_mqtt_ip" android:maxLines="1" android:selectAllOnFocus="true" android:singleLine="true" android:title="@string/pref_title_mqtt_ip" android:summary="@string/pref_defult_mqtt_ip" />

        <EditTextPreference android:capitalize="characters" android:defaultValue="@string/pref_defult_mqtt_port" android:inputType="textCapWords" android:key="@string/pref_key_mqtt_port" android:maxLines="1" android:selectAllOnFocus="true" android:singleLine="true" android:title="@string/pref_title_mqtt_port" android:summary="@string/pref_defult_mqtt_port" />

        <EditTextPreference android:capitalize="characters" android:defaultValue="@string/pref_defult_mqtt_topic" android:inputType="textCapWords" android:key="@string/pref_key_mqtt_topic" android:maxLines="1" android:selectAllOnFocus="true" android:singleLine="true" android:title="@string/pref_title_mqtt_topic" android:summary="@string/pref_defult_mqtt_topic" />

        <EditTextPreference android:capitalize="characters" android:defaultValue="@string/pref_defult_mqtt_user" android:inputType="textCapWords" android:key="@string/pref_key_mqtt_user" android:maxLines="1" android:selectAllOnFocus="true" android:singleLine="true" android:title="@string/pref_title_mqtt_user" android:summary="@string/pref_defult_mqtt_user" />

        <EditTextPreference android:capitalize="characters" android:defaultValue="@string/pref_defult_mqtt_pswd" android:inputType="textCapWords" android:key="@string/pref_key_mqtt_pswd" android:maxLines="1" android:selectAllOnFocus="true" android:singleLine="true" android:title="@string/pref_title_mqtt_pswd" android:summary="@string/pref_defult_mqtt_pswd" />
    </PreferenceCategory>
</PreferenceScreen>

使用Header跳转页面

使用preference-headers进行实现,在res/xml文件夹新建pref_headers.xml

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- These settings headers are only used on tablets. -->

    <header android:fragment="com.pax.preference.SettingsActivity$GeneralPreferenceFragment" android:icon="@drawable/ic_info_black_24dp" android:title="@string/pref_header_general" />

    <header android:fragment="com.pax.preference.SettingsActivity$NotificationPreferenceFragment" android:icon="@drawable/ic_notifications_black_24dp" android:title="@string/pref_header_notifications" />

    <header android:fragment="com.pax.preference.SettingsActivity$DataSyncPreferenceFragment" android:icon="@drawable/ic_sync_black_24dp" android:title="@string/pref_header_data_sync" />

</preference-headers>

在Activity中设置xml

@Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onBuildHeaders(List<Header> target) { 
   
        loadHeadersFromResource(R.xml.pref_headers, target);
    }

进行平板适配,这种方式可以适配平板显示。

private static boolean isXLargeTablet(Context context) { 
   
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
    }

平板显示效果
在这里插入图片描述
手机显示效果
在这里插入图片描述

致谢

感谢观看,欢迎评论和点赞!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/196320.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 详述 Spring MVC 框架中拦截器 Interceptor 的使用方法

    详述 Spring MVC 框架中拦截器 Interceptor 的使用方法1前言  昨天新接了一个需要,“拦截XXX,然后OOO”,好吧,说白了就是要用拦截器干点事(实现一个具体的功能)。之前,也在网络上搜了很多关于Interceptor的文章,但感觉内容都大同小异,而且知识点零零散散,不太方便阅读。因此,正好借此机会,整理一篇关于拦截器的文章,在此分享给大家,以供大家参考阅读。2拦截器2.1概念  Java里的拦截器是动态拦截action调用的对象。它提

    2022年5月14日
    33
  • SAP ABAP计划 SY-REPID与SY-CPROG差异

    SAP ABAP计划 SY-REPID与SY-CPROG差异

    2021年12月30日
    44
  • Pycharm git 使用简介

    Pycharm git 使用简介1.VCS版本控制服务器(GITCVSSVN)2.VCS—&gt;checkoutfromversioncontrol从服务器clone代码并且创建本地项目,默认切换到主分支(master)3.gitignore文件:用来添加忽略文件4.右键—&gt;Git—&gt;Repository—&gt;Branches创建并切换到dev分支5.开发到一定阶段将dev分支…

    2022年10月24日
    0
  • 流水线设计思想_全自动流水线

    流水线设计思想_全自动流水线在硬件电路设计中,流水线设计思想是一种很重要的设计思想,这种思想是一种用面积换速度的思想,用更多的资源来实现高速。(面积就是需要的硬件数量,如触发器的数量)顾名思义,流水线思想,就像工厂中的流水线一样。假设是一个手机组装的流水线,一个三个步骤:A,将电池装入手机起来;B,将屏幕组装起来;C,将外壳组装起来。在上面的三个步骤中,流水线的实现就是:A步骤实现后,将手机发往B,然后A继续组装电池,而不会等待C完成再组装;B和C也是一样。流水线思想就是自己完成自己的功能,不会等待。这在硬件电路中就是一种并行的

    2022年8月14日
    1
  • mui框架从0到1【webapp开发教程】

    mui框架从0到1【webapp开发教程】随着需求的不断更新,与对技术的不断探索,计划在20天之内与团队协作开发一款移动端的app

    2022年6月15日
    33
  • Clipper库中文文档(ClipperLib)

    Clipper库中文文档(ClipperLib)中文文档链接:https://love2.io/@martinchan3/doc/ClipperDocCNGit仓库:https://github.com/MartinChan3/ClipperDocCNClipper是计算机图形学中常用的库,支持Delphi、C++、C#等多种语言。之前看英文原文文档(http://www.angusj.com/delphi/clipper/docu…

    2025年5月25日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号