安卓中preferenceFragment的使用

安卓中preferenceFragment的使用文章目录 PreferenceFr 是什么 Preferences 页面 string 页面 gt 我们来写 UI 界面接着我们来写一个 MainActivity 页面最后我们来写 FragmentPerf 页面 PreferenceFr 是什么 publicabstra 以一个列表来展示首选项对象

PreferenceFragment是什么

public abstract class PreferenceFragment extends Fragment

以一个列表来展示首选项对象的层级关系,这些首选项将自动地保存为SharedPreferences,使用户可以用他们来进行交互。为了能够重新获得ShaedPreferences的实例,该Fragement中的层级首选项将会在同一个包下面使用带有一个上下文的PreferenceManager.getDefaultSharedPreferences作为这个fragement 。

此外,所展示的首选项将会遵循系统首选项的视觉风格,通过使用XML文件来创建各个首选项的视图层级(可以被显示在许多页面)会非常简单。基于上述原因,推荐在应用中使用这个fragement(作为一个超类)来处理首选项问题。

一个PreferenceScreen对象应该在首选项层级的顶部。此外,随后在层次结构PreferenceScreen表示一个屏幕分割处——就是包含随后的PreferenceScreen应显示在另一个屏幕页面上。首选项框架处理从首选项层次结构显示了这些其他屏幕内容。

首选项层次结构可以有很多种方式形成:

●从一个XML文件制定的层次结构。

●从不同的activity,每一个activity通过meta-data在一个XML文件中制定他自己的首选项。

●从一个以PreferenceScreen为根的层次结构对象。

为了从一个XML文件中获取界面,使用addPreferenceFromResource(int)方法。根元素应该使用PreferenceScreen。随后的元素可以指向实际的首选项的子类。正如上面提到的,在层次结构中随后的PreferenceScreen将导致屏幕分割处。

为了指定一个意图来查询都带有各自首选项的activitiy,使用addPreferenceFromIntent方法。每个activity可以在manifest文件中指定meta-data来指向一个XML文件资源。这些资源文件将被填充到单独的首选项层次结构并且通过这个fragment来展示。

为了指定一个以PreferenceScreen为根元素的对象,使用setPreferenceScreen(PreferenceScreen)方法。

方便起见,这个fragment实现了一个用于当前层次结构中任意首选项的点击事件监听器,onPreferenceTreeClick(PreferenceScreen,Preference).

以上翻译自PreferenceFragment的官方文档,可自行查阅其原版说明

Preferences页面

<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="In-line preferences"> <CheckBoxPreference android:key="checkbox_preference" android:summary="@string/s1" android:title="@string/s2" /> </PreferenceCategory> <PreferenceCategory android:title="Dialog-based preferences"> <EditTextPreference android:dialogTitle="@string/d1" android:key="edittext_preference" android:summary="@string/d2" android:title="@string/d3" /> <!--android:entries设置的内容是我们能够看到的内容,而android:entryValues是实际保存的值。--> <ListPreference android:dialogTitle="@string/f1" android:entries="@array/entries_list_preference" android:entryValues="@array/entryvalues_list_preference" android:key="list_preferenc" android:summary="@string/f2" android:title="@string/f3" /> </PreferenceCategory> <PreferenceCategory android:title="Launch preferences"> <PreferenceScreen android:key="screen_preference" android:summary="@string/g1" android:title="@string/g2"> <!-- 你可以在这里放置更多的首选项内容,将被在下一个页面呈现出来 --> <CheckBoxPreference android:key="next_screen_checkbox_preference" android:summary="@string/g3" android:title="@string/g4" /> </PreferenceScreen> <PreferenceScreen android:summary="@string/h1" android:title="@string/h2"> <intent android:action="android.intent.action.VIEW" android:data="http://www.baidu.com" /> </PreferenceScreen> </PreferenceCategory> <PreferenceCategory android:title="Preference attributes"> <CheckBoxPreference android:key="parent_checkbox_preference" android:summary="@string/j1" android:title="@string/j2" /> <!-- 子类的可见类型是由样式属性定义的 --> <CheckBoxPreference android:dependency="parent_checkbox_preference" android:key="child_checkbox_preference" android:layout="?android:attr/preferenceLayoutChild" android:summary="@string/k1" android:title="@string/k2" /> </PreferenceCategory> </PreferenceScreen> 

string页面

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.beta.perferencefragmenttest.MainActivity"> <Button android:id="@+id/per" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="从这里跳转到设置PreferenceFragment"/> </android.support.constraint.ConstraintLayout> 

> 我们来写UI界面

activity_main.xml这个界面很简单的就一个BUTTON用于简单的跳转

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.beta.perferencefragmenttest.MainActivity"> <Button android:id="@+id/per" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="从这里跳转到设置PreferenceFragment"/> </android.support.constraint.ConstraintLayout> 

接着我们来写一个MainActivity页面

功能就不分析了就只有一个button用于跳转

import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button bt_per = (Button)findViewById(R.id.per); bt_per.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent1 = new Intent(MainActivity.this,FragmentPreferences.class); startActivity(intent1); } }); } } 

最后我们来写FragmentPerfences页面

import android.app.Activity; import android.os.Bundle; import android.preference.PreferenceFragment; public class FragmentPreferences extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //从FragmentManager管理其中得到并且开始传输创建一个新的PerfencesFragment getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragement()).commit(); } //这个PrefsFragment继承PreferenceFragment /*然后我们把我们之前写好的preferences文件给它加载进去那么我们就成功了获取到设置页面的东西了。*/ public static class PrefsFragement extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } } } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
上一篇 2025年9月16日 下午8:01
下一篇 2025年9月16日 下午8:22


相关推荐

  • 路由懒加载的原理及实现_前端路由懒加载

    路由懒加载的原理及实现_前端路由懒加载懒加载解决的问题:避免进入首页就加载全部的前端资源造成用户等待时间过长的问题。就好比,访问login页面,你返回的js路由不仅有渲染login页面的,还有渲染production页面以及其他页面的功能。而这些代码量太大了,文件也大。js文件有个特征,下载完全了才会运行,导致页面首屏速度太慢了,也就是白屏时间太长。这个问题,早就有人发现,于是解决方案就是路由懒加载,这只是一个技术名词。Vue路由懒加载原理说明1)我们一开始用ES6的写法,在路由文件router/index.js中

    2026年4月15日
    7
  • idea 2021.5激活码【在线注册码/序列号/破解码】[通俗易懂]

    idea 2021.5激活码【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    45
  • 解决Ubuntu 12.04 xfce 宋体英文发虚

    解决Ubuntu 12.04 xfce 宋体英文发虚为什么 80 的码农都做不了架构师 gt gt gt

    2026年3月18日
    3
  • python 爬虫 通过搜索引擎搜索好看的图片进行多线程高效率爬取(解决href关联问题)

    python 爬虫 通过搜索引擎搜索好看的图片进行多线程高效率爬取(解决href关联问题)效果:单线程模式:#!/usr/bin/envpython#-*-coding:utf-8-*-#@Time:2020/12/3018:56#@Author:huni#@File:图集谷单函数.py#@Software:PyCharmimportrequestsfromlxmlimportetreefromurllibimportparseimportosif__name__==’__main__’:h

    2022年7月17日
    23
  • DB2错误代码_db2错误码57016

    DB2错误代码_db2错误码570161前言作为一个程序员,数据库是我们必须掌握的知识,经常操作数据库不可避免,but,在写SQL语句的时候,难免遇到各种问题。例如,当咱们看着数据库报出的一大堆错误代码时,是否有种两眼发蒙的感觉呢?咳咳,莫要否认,你有、我有,全都有啊!不过,值得庆幸的是,已经有人帮咱们整理出一份关于DB2的错误代码大全啦,以后再遇到数据库报错,直接拎出看看,岂不爽哉?当然,在此对原作者送上万分的感谢。2错误

    2025年12月14日
    8
  • Spark Streaming Join「建议收藏」

    Spark Streaming Join「建议收藏」多数据源Join思路多数据源Join大致有以下三种思路:数据源端Join,如Android/IOS客户端在上报用户行为数据时就获取并带上用户基础信息。计算引擎上Join,如用SparkStreaming、Flink做Join。结果端Join,如用HBase/ES做Join,Join键做Rowkey/_id,各字段分别写入列簇、列或field。三种思路各有优劣,使用时注意…

    2022年6月30日
    29

发表回复

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

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