android设置页面之PreferenceActivity及Preference

article/2025/10/29 22:36:50
离上篇博客刚好一周,希望后面会记录更多的内容,也算自己的Android笔记吧。
    本篇主要记录一般的android设置页面PreferenceActivity的使用以及与之剪不断,理还乱的Preference。
   (一)如何使用
    Android系统自带的设置应用就是利用PreferenceActivity构建的。PreferenceActivity是一个抽象类,继承于ListActivity,通过一个列表视图来展现配置页面,自己的Activity继承PreferenceActivity,在onCreate方法中由两种方式来设置列表页面,一是XML文件,使用addPreferencesFromResource(XML资源id);二是code方式,使用setPreferenceScreen(PreferenceScreen)。其实质都是由一个PreferenceScreen作为根来展开。而API中提供的现成的设置控件有:CheckBoxPreference、EditTextPreference、ListPreference、RingtonePreference以及后来的MultiSelectListPreference(API11),SwitchPreference(API14)。从XML方式显示来举个例子,相关属性都有对应的函数,所以code方式就类似了。我这里以android2.2源码中的设置为范本,在讲述如何使用系统的设置控件的同时,简单描述下自定义设置控件的使用,在2.2中设置页面还不带icon属性,需要自己定义。

    先拥有一个Settings的类继承于PreferenceActivity,加载的XML资源文件为settings.xml(其位置自己定义),代码如下:

package com.example.settings;import android.os.Bundle;
import android.preference.PreferenceActivity;public class Settings extends PreferenceActivity { @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.settings);}
}

    接着定义settings.xml,根为PreferenceScreen,两个PreferenceCategory,第一个PreferenceCategory下是自定义的带icon属性项,第二个则是一个普通的preference和一个Checkbox代码如下,可以看下官网的属性定义(这里),方便理解:

<?xml version="1.0" encoding="utf-8"?>
<!--Copyright (C) 2008 The Android Open Source ProjectLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
--><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"xmlns:settings="http://schemas.android.com/apk/res/com.example.settings"android:key="parent"android:title="设置页面" ><PreferenceCategory android:title="自定义" ><com.example.settings.IconPreferenceScreenandroid:title="通用设置"settings:icon="@drawable/ic_settings_about" ><intentandroid:action="android.intent.action.MAIN"android:targetClass="com.example.settings.Settings"android:targetPackage="com.example.settings" /></com.example.settings.IconPreferenceScreen></PreferenceCategory><PreferenceCategory android:title="系统" ><Preferenceandroid:icon="@drawable/ic_settings_about"android:title="关于" ></Preference><CheckBoxPreferenceandroid:defaultValue="true"android:summaryOff="没选中"android:summaryOn="选中了!"android:title="选中了吗?" ></CheckBoxPreference></PreferenceCategory></PreferenceScreen>

    IconPreferenceScreen代码和对应的XML文件代码如下:

/** Copyright (C) 2009 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.example.settings;import com.example.settings.R;import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;public class IconPreferenceScreen extends Preference {private Drawable mIcon;public IconPreferenceScreen(Context context, AttributeSet attrs) {this(context, attrs, 0);}public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);setLayoutResource(R.layout.preference_icon);TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.IconPreferenceScreen, defStyle, 0);mIcon = a.getDrawable(R.styleable.IconPreferenceScreen_icon);}@Overridepublic void onBindView(View view) {super.onBindView(view);ImageView imageView = (ImageView) view.findViewById(R.id.setting_icon);if (imageView != null && mIcon != null) {imageView.setImageDrawable(mIcon);}}
}

    preference_icon.xml:

<?xml version="1.0" encoding="utf-8"?>
<!--Copyright (C) 2006 The Android Open Source ProjectLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:minHeight="?android:attr/listPreferredItemHeight"android:paddingRight="?android:attr/scrollbarSize" android:orientation="horizontal"><ImageViewandroid:id="@+id/setting_icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginLeft="6dip"android:layout_marginRight="6dip" /><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="6dip"android:layout_marginLeft="2dip"android:layout_marginRight="6dip"android:layout_marginTop="6dip"android:layout_weight="1" ><TextViewandroid:id="@android:id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ellipsize="marquee"android:fadingEdge="horizontal"android:singleLine="true"android:textAppearance="?android:attr/textAppearanceLarge" /><TextViewandroid:id="@android:id/summary"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@android:id/title"android:layout_below="@android:id/title"android:maxLines="2"android:textAppearance="?android:attr/textAppearanceSmall" /></RelativeLayout>    </LinearLayout>

    显示界面如下:

    
   需要说明的是,你得为你的IconPreferenceScreen定义自定义属性icon,在attrs.xml文件中,代码如下,这也是你settings.xml中settings属性的来源及和IconPreferenceScreen代码中mIcon对应的资源。
   
<resources><declare-styleable name="IconPreferenceScreen"><attr name="icon" format="reference" /></declare-styleable>

(二)Preference的理解

    首先理解自定义的Icon是从onBindView中添加到了设置页面,它是如何做到的呢。PreferenceActivity继承ListActivity,而其中包含的ListView都会通过Adapter的getView方法来获得要显示的视图,从Preference中的源码可以发现,其getView方法会调用onBindView方法,而getView方法是会被PreferenceGroupAdapter的getView方法所调用的。
    接下来是Preference拥有保存设置值的能力,它是由PreferenceManager来统一管理的,咱们调用persistInt,persistString将值保存到SharedPreferences的xml文件中,对应的key就是在XML中定义的属性key,默认的SharedPreferences的xml文件名为应用的包名+"_preferences"。相应的我们获取该保存值也就可以通过getDefaultSharedPreferences或getSharedPreferences获得SharedPreferences后调用键值对的获取。
    最后理一下Preference思路,它类似于View。你看PreferenceScreen间接继承Preference,其成员ListAdapter为PreferenceGroupAdapter类型,负责设置内容的显示与更新;实现了 AdapterView.OnItemClickListener,当某个Item被点击时,调用该item的performClick,实现targetClass对应的Activity的启动;而具体的控件CheckBoxPreference继承Preference,根据自己的特点实现相应的方法。

    先到这吧,本来想详细的说说Preference之间的关系的,结果使用示例内容多了,以后再和ListActivity对比的理解吧。

    使用的示例可以看看:

    (1)Android软件开发之PreferenceActivity中的组件(二十八)

    (2)Android的设置界面及Preference使用


http://chatgpt.dhexx.cn/article/mC5KkZXe.shtml

相关文章

android中PreferencesActivity的使用(一)

在使用android手机的时候&#xff0c;尤其是在操作软件设置时&#xff0c;我们经常见到这样的界面&#xff1a; 这是怎么来实现的的呢&#xff1f;其实android已经提供了相应的类和方法&#xff0c;当进行简单数据存储时&#xff08;比如&#xff1a;软件配置参数&#xff09;a…

PreferenceActivity用法简介(二)

本测试主要是为了测试PreferenceActivity的使用&#xff0c;其中设置了播放背景音乐和开启wifi的设置&#xff0c;也就是本文要讲的 PreferenceActivity。 Android提供了放摆放的工具来定义所有的程序的首选项&#xff0c;并支持既不不许要编写代码的情况写显示这些首选项。可…

Android之PreferenceActivity

看到很多书中都没有对PreferenceActivity做介绍&#xff0c;而我在看Android Samples时无意中看见了&#xff0c;所以就稍微总结一下&#xff0c;也方便日后查找。 PerferenceActivity是什么&#xff0c;看下面的截图&#xff1a; 好了&#xff0c;我们看到Android系统本身就大…

Android PreferenceActivity使用

PreferenceActivity继承了ListActivity&#xff0c;定义Activity继承PreferenceActivity。在res目录下新建一个xml文件夹&#xff0c;接着在这个文件夹下新建一个取名为preferences.xml的File文件&#xff0c;xml中可以使用的标签(Tag)可以分为两类&#xff0c;一类是管理布局的…

PreferenceActivity定制

android很多设置界面都会使用PreferenceActivity来实现&#xff0c;但那个界面比较丑陋&#xff0c;显示开发总是满足不了要求。 可以自己实现一个&#xff0c;但是那样又会使Activity中的逻辑代码和xml布局文件过于复杂&#xff0c;远远不及PreferenceActivity来的方便快捷。…

PreferenceActivity、PreferenceFragment使用

目录 目录前言PreferenceActivity preferences_scenario_1xmlPreference Activity演示 PreferenceFragment xml布局文件Preference FragmentPreference Activity管理Fragment 适配 前言 转来转去又回到了Android&#xff0c;闲话少说&#xff0c;这里是参考Android原生的Setti…

Android PreferenceActivity的介绍

转载&#xff1a;http://blog.chinaunix.net/uid-24666775-id-351136.html 在Android中的APIdemos是中经常遇到过继承于PreferenceActivity这个类&#xff0c;紧接着就是addPreferencesFromResource(R.xml.*******);&#xff08;附&#xff1a;这个******就是一个XML文件&#…

Android开发--详解SharedPreference/PreferenceActivity

我们经常看到应用程序的设置页面&#xff0c;一般用到设置页面时&#xff0c;我们会继承自PreferenceActivity&#xff0c;它实现了SharedPreference&#xff0c;并生成相应的XML文件自动保存用户的设置&#xff0c;在设置页面中&#xff0c;每一个列表项都是一个Preference&am…

Android之PreferenceActivity 详解

看到很多书中都没有对PreferenceActivity做介绍&#xff0c;而我正好又在项目中用到&#xff0c;所以就把自己的使用的在这总结一下&#xff0c;也方便日后查找。 PerferenceActivity是什么&#xff0c;看下面的截图&#xff1a; Android系统截图&#xff08;左&#xff09; …

android的PreferenceActivity

前言 这段时间在研究android平台上的开源项目——StandupTimer&#xff0c;这是由jwood所设计的一个较为简单android应用&#xff0c;用于控制会议时间&#xff0c;类似秒表倒计时。 PreferenceActivity PreferenceActivity是android提供的对系统信息和配置进行自动保存的Activ…

用StopWatch统计耗时,比System.currentTimeMillis好用

平时项目中统计耗时都用System.currentTimeMillis&#xff0c;最近看到一个spring-StopWatch统计耗时&#xff0c;其用法简单明了&#xff0c;比传统统计耗时方法好用。 StopWatch 的内部是通过 System.nanoTime() 来计时的&#xff0c;本质和 System.currentTimeMillis() 差别…

并发慎用——System.currentTimeMillis()

好记忆不如烂笔头&#xff0c;能记下点东西&#xff0c;就记下点&#xff0c;有时间拿出来看看&#xff0c;也会发觉不一样的感受. System.currentTimeMillis()是极其常用的基础Java API&#xff0c;广泛地用来获取时间戳或测量代码执行时长等&#xff0c;在我们的印象中应该快…

System.currentTimeMillis()的性能问题以及解决方法

System.currentTimeMillis()是极其常用的基础Java API&#xff0c;广泛地用来获取时间戳或测量代码执行时长等&#xff0c;在我们的印象中应该快如闪电。但实际上在并发调用或者特别频繁调用它的情况下&#xff08;比如一个业务繁忙的接口&#xff0c;或者吞吐量大的需要取得时…

统计代码执行时间时,System.currentTimeMillis()与System.nanoTime()哪个更适合?

目录 1.nanoTime是什么&#xff1f; 2.currentTimeMillis是什么&#xff1f; 3.nanoTime与currentTimeMillis在JDK中阐述 4.nanoTime与currentTimeMillis使用对比 5.深究从OpenJDK源代码、glibc&#xff0c;一直到Linux内核 6.总结 1.nanoTime是什么&#xff1f; ns&…

System.currentTimeMillis的性能如何

一、背景 撸代码时发现System.currentTimeMillis的调用都被封装成了cache类型&#xff0c;代码如下: 那么System.currentTimeMillis真的有这么这么差吗&#xff0c;如果差的话又是什么原因造成的&#xff1f;什么情况下可以直接调用原生方法&#xff0c;什么情况下需要使用缓存…

别再用 System.currentTimeMillis 统计耗时了,太 Low,试试 Spring Boot 源码在用的 StopWatch吧,够优雅

大家好&#xff0c;我是二哥呀&#xff01; 昨天&#xff0c;一位球友问我能不能给他解释一下 SpringBootApplication 注解是什么意思&#xff0c;还有 Spring Boot 的运行原理&#xff0c;于是我就带着他扒拉了一下这个注解的源码&#xff0c;还有 SpringApplication 类的 ru…

System.currentTimeMillis的性能,真有如此不堪吗?

# 疑惑&#xff0c;System.currentTimeMillis真有性能问题&#xff1f; 最近我在研究一款中间件的源代码时&#xff0c;发现它获取当前时间不是通过System.currentTimeMillis&#xff0c;而是通过自定义的System.currentTimeMillis的缓存类&#xff08;见下方&#xff09;&…

疑惑,System.currentTimeMillis真有性能问题?

点击关注公众号&#xff0c;Java干货及时送达 System.currentTimeMillis的性能真有如此不堪吗&#xff1f; 最近我在研究一款中间件的源代码时&#xff0c;发现它获取当前时间不是通过System.currentTimeMillis&#xff0c;而是通过自定义的System.currentTimeMillis的缓存类&a…

高并发下System.currentTimeMillis()性能问题及优化方案

文章目录 背景System.currentTimeMillis()性能测试单线程测试多线程测试 原因优化优化代码单线程测试多线程测试 参考 背景 最近在看asyncTool源码发现了System.currentTimeMillis存在卡顿问题&#xff0c;所以就详细研究了下。具体如何呢&#xff1f;我们来看看 System.curr…

currentTimeMillis()方法

currentTimeMillis()方法返回一个long类型的值&#xff0c;该值表示的是当前时间与1970年1月1日0时0分0秒之间的时间差&#xff0c;单位是毫秒&#xff0c;习惯上被称为时间戳 源码&#xff1a; 时间戳可以用来计算循环操作时所需要的时间&#xff1a; /*** 向goods表中插入…