安卓中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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 浅谈 HTTP 和 HTTPS[通俗易懂]

    浅谈 HTTP 和 HTTPS[通俗易懂]HTTP作为我们浏览器与服务器之间通讯协议,目前这块知识成为了面试的高频率题,也是我们前后端同学必须掌握的部分,一起来看看吧!

    2022年10月16日
    8
  • 打造自己的ip代理池

    打造自己的ip代理池在爬虫时经常需要使用代理,于是我爬取了一个可以免费提供代理的网址,从中获取免费代理,从而打造属于一个自己的代理池。如图所示,这是网址的界面展示,我们需要做的就是需要其中的ip、port列中的数据,获取数据后需要我们拼接成一个完整的IP然后保存即可,代码如下:importrequestsfromlxmlimportetreeimportosurl=’https://www.kuaidaili.com/free/inha/3/’headers=…

    2022年5月31日
    60
  • Linux 查看内存使用情况

    Linux 查看内存使用情况

    2022年2月13日
    44
  • 请不要拿ipad和手机、上网本相提并论「建议收藏」

    请不要拿ipad和手机、上网本相提并论「建议收藏」苹果公司最近推出了ipad,但网上的评论不是很好,我觉得,一个好的产品会改变人们对某些事物的认识,甚至是人们的生活习惯。网上有ipad的十大罪状,有些我不了解,但有我认为是无稽之谈。不支持多点触摸首先,多点触摸苹果公司在他们的产品中应用的比较多的,比如iphone和macair,当然这样的技术迁移到ipad上面肯定没有任何问题,但是,网上认为这样的技术在ipad的设计上被取消

    2022年9月23日
    3
  • php 全部替换字符串,php如何批量替换字符串

    php 全部替换字符串,php如何批量替换字符串php如何批量替换字符串2020-10-0614:54:06php批量替换字符串的方法:使用【str_replace】批量查找替换字符串,代码为【$str=str_replace(‘o’,’O’,$str,$count);echo$str.PHP_EOL;】。php批量替换字符串的方法:str_replace批量查找替换字符串…

    2022年5月10日
    45
  • Java数组转Json数组「建议收藏」

    Java数组转Json数组「建议收藏」packagecom.cnic.test.coding;importcom.alibaba.fastjson.JSONArray;publicclassArrToJson{publicstaticvoidmain(String[]args){int[]my=newint[5];my[0]=0;my[1]=1;my[2]=2;my[3]=3;.

    2022年6月21日
    86

发表回复

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

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