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


相关推荐

  • java uuid 随机数_Java随机数和UUID[通俗易懂]

    java uuid 随机数_Java随机数和UUID[通俗易懂]Java随机数和UUID#Java随机数在Java项目中通常是通过Math.random方法和Random类来获得随机数,前者通过生成一个Random类的实例来实现。此类产生的是一组伪随机数流,通过使用48位的种子,利用线性同余公式产生。在Java中,随机数的产生取决于种子,随机数和种子之间的关系遵从以下两个规则:种子不同,产生不同的随机数。种子相同,即使实例不同也产生相同的随机数。两种方式设…

    2022年7月14日
    17
  • Bitmap.MakeTransparent 方法

    Bitmap.MakeTransparent 方法使默认的透明颜色对此Bitmap透明。重载列表使默认的透明颜色对此Bitmap对象透明。[Visual Basic]OverloadsPublicSubMakeTransparent()[C#]publicvoidMakeTransparent();[C++]public:voidMakeTransparent();[JScript]publicfunctionM

    2022年7月21日
    13
  • 第一期打卡送书5本+1个腾讯视频VIP月卡(11月1日-12月1日)

    第一期打卡送书5本+1个腾讯视频VIP月卡(11月1日-12月1日)

    2020年11月14日
    187
  • bigdecimal保留两位小数,不够两位补0_如何保留两位小数

    bigdecimal保留两位小数,不够两位补0_如何保留两位小数BigDecimal保留两位小数核心方法详解:/**BigDecimal.setScale()方法用于格式化小数点setScale(1)表示保留一位小数,默认用四舍五入方式*setScale(1,BigDecimal.ROUND_DOWN)直接删除多余的小数位,如2.35会变成2.3*setScale(1,BigDecimal.ROUND_UP)进位处理,2.35变成2.4*setScale(1,BigDecimal.ROUND_HALF_UP)四舍五入,2.35变成2.4*

    2022年9月16日
    3
  • API之FindWindowEx和SendMessage

    API之FindWindowEx和SendMessage最近在VC6.0开发中碰到了两个函数,经过一番搜索查阅,特记录于此。

    2022年5月27日
    35
  • futex机制介绍「建议收藏」

    futex机制介绍「建议收藏」1、概念futex:asortoffast,user-spacemutualexclusionprimitive. Futex是一种用户态和内核态混合的同步机制。首先,同步的进程间通过mmap共享一段内存,futex变量就位于这段共享的内存中且操作是原子的,当进程尝试进入互斥区或者退出互斥区的时候,先去查看共享内存中的futex变量,如果没有竞争发生,则只修改futex,而不用…

    2022年4月19日
    70

发表回复

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

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