PreferenceActivity中的组件

PreferenceActivity中的组件PreferenceAc 介绍 PreferenceAc 继承 ListActivity 它是以一个列表的形式在展现内容 它最主要的特点是添加 Preference 可以让控件的状态持久化储存 举个例子比如用户选中 checkbox 后退出应用然后在进入应用 这时用户希望看到的是 checkbox 被选中 所以软件须要记录用户每次操作的过程并且持久储存 在进入应用的时候须要判断

PreferenceActivity 介绍 :

PreferenceActivity 继承ListActivity 它是以一个列表的形式在展现内容,它最主要的特点是添加Preference可以让控件的状态持久化储存,举个例子 比如用户选中checkbox后 退出应用然后在进入应用,这时用户希望看到的是checkbox被选中,所以软件须要记录用户每次操作的过程并且持久储存,在进入应用的时候须要判断这些久储存的数据然后将系统控件的状态呈现在UI中。
尤其是软件开发肯定会有一堆设置选项选项,每次进入Activity都去手动的去取储存的数据,这样代码会变得很复杂很麻烦。 这个时候Preference就出来了,它就是专门解决这些特殊的选项保存与读取的显示。用户每次操作事件它会及时的以键值对的形式记录在SharedPreferences中,Activity每次启动它会自动帮我们完成数据的读取以及UI的显示。
android开发中一共为我们提供了4个组件,分别是CheckBoxPreference组件、EditTextPreference组件、ListPreference组件、RingtonePreference组件,下面我用一个例子一一向同学们介绍一下。
工程结构图:
[img]
[img]http://dl.iteye.com/upload/attachment/0062/1951/defc7e3a-34fd-37b1-9a91-b4c281.png[/img]
[/img]
运行效果图:
[img]
[img]http://dl.iteye.com/upload/attachment/0062/1953/4e4dbff9-3674-313f-8822-868f529ede25.png[/img]
[/img]

main.xml

 
                
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
android:src="@drawable/jay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="18dip"
android:background="#00FF00"
android:text="雨松MOMO 带你走进Android 软件开发的世界"
android:gravity="center_vertical|center_horizontal"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="18dip"
android:background="#0000FF"
android:text="Android软件开发之PreferenceActivity中组件的使用(二十八)"
android:gravity="center_vertical|center_horizontal"
/>


























































































ControlActivity

package cn.m15.xys;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/
*
* @author 无敌小胖胖
*
*
*/
public class ControlActivity extends Activity {

Context mContext = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
/CheckBoxPreference/
Button botton0 = (Button)findViewById(R.id.button0);
botton0.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(mContext,CheckBoxActivity.class);
startActivity(intent);
}
});
/EditTextPreference/
Button botton1 = (Button)findViewById(R.id.button1);
botton1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(mContext,EditTextActivity.class);
startActivity(intent);
}
});

/ListPreference/
Button botton2 = (Button)findViewById(R.id.button2);
botton2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(mContext,ListActivity.class);
startActivity(intent);
}
});
/RingtonePreference/
Button botton3 = (Button)findViewById(R.id.button3);
botton3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(mContext,RingtoneActivity.class);
startActivity(intent);
}
});
/all/
Button botton4 = (Button)findViewById(R.id.button4);
botton4.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(mContext,AllActivity.class);
startActivity(intent);
}
});
}
}


































































































































































































































CheckBoxPreference组件
CheckBoxPreference 选中为true 取消选中为false 它的值会以boolean的形式储存在SharedPreferences中。

checkbox.xml

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

android:title="CheckBox_A"
android:summary="这是一个勾选框A" >








android:title="CheckBox_B"
android:summary="这是一个勾选框B" >


























CheckBoxActivity

package cn.m15.xys;


import android.content.Context;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;

public class CheckBoxActivity extends PreferenceActivity {

Context mContext = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences
addPreferencesFromResource(R.xml.checkbox);

mContext = this;

//CheckBoxPreference组件
CheckBoxPreference mCheckbox0 = (CheckBoxPreference) findPreference("checkbox_0");
mCheckbox0.setOnPreferenceClickListener(new OnPreferenceClickListener() {

@Override
public boolean onPreferenceClick(Preference preference) {
//这里可以监听到这个CheckBox 的点击事件
return true;
}
});

mCheckbox0.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

@Override
public boolean onPreferenceChange(Preference arg0, Object newValue) {
//这里可以监听到checkBox中值是否改变了
//并且可以拿到新改变的值
Toast.makeText(mContext, "checkBox_0改变的值为" + (Boolean)newValue, Toast.LENGTH_LONG).show();
return true;
}
});

CheckBoxPreference mCheckbox1 = (CheckBoxPreference) findPreference("checkbox_1");
mCheckbox1.setOnPreferenceClickListener(new OnPreferenceClickListener() {

@Override
public boolean onPreferenceClick(Preference preference) {
//这里可以监听到这个CheckBox 的点击事件
return true;
}
});

mCheckbox1.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

@Override
public boolean onPreferenceChange(Preference arg0, Object newValue) {
//这里可以监听到checkBox中值是否改变了
//并且可以拿到新改变的值
Toast.makeText(mContext, "checkBox_1改变的值为" + (Boolean)newValue, Toast.LENGTH_LONG).show();
return true;
}
});

}

}












































































































































































































EditTextPreference组件
EditTextPreference 点击后会弹出一个输入框,输入的内容会以字符串的的形式储存在SharedPreferences中。
edittext.xml

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

android:title="输入信息_A"
android:summary="请输入您的信息"
android:defaultValue="请输入信息"
android:dialogTitle="输入框">














android:title="输入信息_B"
android:summary="请输入您的信息"
android:defaultValue="请输入信息"
android:dialogTitle="输入框">
































EditTextActivity

package cn.m15.xys;


import android.content.Context;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;

public class EditTextActivity extends PreferenceActivity {

Context mContext = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences
addPreferencesFromResource(R.xml.edittext);

mContext = this;

// EditTextPreference组件
EditTextPreference mEditText = (EditTextPreference) findPreference("edit_0");

//设置dialog按钮信息
mEditText.setPositiveButtonText("确定");
mEditText.setNegativeButtonText("取消");

//设置按钮图标
mEditText.setDialogIcon(R.drawable.jay);
}


}
































































































ListPreference组件
在res/array中先写两个数组,一个用与list的显示内容,一个用户list的选中数值。
ListPreference点击后会弹出一个列表框,选中后会将选中的内容(上面数组中的值)会以字符串的的形式储存在SharedPreferences中。
list.xml

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

android:key="list_0"
android:title="登录设置A"
android:dialogTitle="选择在线时间"
android:entries="@array/auto_logout_time_key"
android:entryValues="@array/auto_logout_time_value" >

















android:key="list_0"
android:title="登录设置A"
android:dialogTitle="选择在线时间"
android:entries="@array/auto_logout_time_key"
android:entryValues="@array/auto_logout_time_value" >



































res/valus/array.xml

 
                                         



10 mins.
20 mins.
30 mins.
60 mins.

















































ListActivity

package cn.m15.xys;


import android.os.Bundle;
import android.preference.PreferenceActivity;

public class ListActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences
addPreferencesFromResource(R.xml.list);
}
}







































RingtonePreference组件
RingtonePreference点击后会弹出一个系统铃声的列表框,选中后会将选中的内容(uri字符集)会以字符串的的形式储存在SharedPreferences中。
android:ringtoneType 系统一共提供了4中响铃模式的类型分别为 铃声(ringtone) 通知( notification) 警告(alarm) 全部(all)
模拟器默认是没有铃声的,下图中的铃声我是将歌曲文件拷贝到SD卡中,设置铃声后才会出现的。如果觉得拷贝麻烦可以使用豌豆荚或者91助手将歌曲文件放入手机SD卡中,在铃声设置那里设置一下在这里就会出现。
ringtone.xml

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

android:key="ringtone_0"
android:summary="选择系统铃声A"
android:title="铃声设置"
android:ringtoneType="all"
android:showSilent="true" >













android:key="ringtone_!"
android:summary="选择系统铃声B"
android:title="铃声设置"
android:ringtoneType="all"
android:showSilent="true" >



































RingtoneActivity

package cn.m15.xys;


import android.os.Bundle;
import android.preference.PreferenceActivity;

public class RingtoneActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences
addPreferencesFromResource(R.xml.ringtone);
}
}







































自定义控件
使用系统的控件在显示方面难免会有些单一,如果想做一个好看的界面就需要使用自定义Preference。下面我简单说明一下如何编写自定义Preference。首先在res/layout中添加preferences文件
res/layout/preferencestyle.xml

 
                                                         
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000">
android:gravity="center_vertical"
android:background="@drawable/preference_mid_background"

android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/setting_about_us">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="6dip"
android:layout_marginRight="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1"
>
android:textSize="15dip"
android:textColor="#000000"
android:ellipsize="marquee"
android:id="@+android:id/title"
android:fadingEdge="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
>

android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#"
android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="4"
android:layout_below="@+android:id/title"
android:layout_alignLeft="@+android:id/title"
>


android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/preference_arrows"/>














































































































































res/drawable/preference_mid_background.xml
android:background=”@drawable/preference_mid_background”
通过这一行可以设置这个按钮的点击、选中默认的显示状态,这样可以让你的按钮更加好看。须要在res/drawable中添加xml文件
android:state_facused :为控件选中显示
android:state_pressed:为控件按下显示
最后一个为默认显示

 
                                                                
xmlns:android="http://schemas.android.com/apk/res/android">
android:state_focused="true"
android:drawable="@drawable/preference_mid_pressed"
>

android:state_pressed="true"
android:drawable="@drawable/preference_mid_pressed"
>


android:drawable="@drawable/preference_mid"
>
















































AllActivity

package cn.m15.xys;


import android.content.Context;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;

public class AllActivity extends PreferenceActivity {

/自定义布局A/
Preference preference0 = null;

/自定义布局B/
Preference preference1 = null;

Context mContext = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences
addPreferencesFromResource(R.xml.all);
mContext = this;

preference0 = findPreference("pref_key_0");

preference0.setOnPreferenceClickListener(new OnPreferenceClickListener() {

@Override
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(mContext, "自定义布局A被按下", Toast.LENGTH_LONG).show();
return false;
}
});
preference1 = findPreference("pref_key_1");

preference1.setOnPreferenceClickListener(new OnPreferenceClickListener() {

@Override
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(mContext, "自定义布局B被按下", Toast.LENGTH_LONG).show();
return false;
}
});
}
}













































































































































读取数据
在PreferenceActivity中可以用下面这种方式拿到SharedPreferences中储存的数值,通过PreferenceManager.getDefaultSharedPreferences(this) 方法拿到控件默认储存的sharedPreferences对象。

 SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(this) ;
boolean something = prefs.getBoolean("something",false);

在模拟起中将SharedPreferences储存内容拷贝出来后,可以清楚的看到通过点击系统控件储存的数值。这里我说一下铃声的储存,它是以一个字符串形式的uri字符集,它所指向的是系统铃声储存的路径。所以根据这个字符集就可以找到这个铃声。

 
                                                                          

content://media/external/audio/media/1
content://media/external/audio/media/1

请输入信息1212

content://settings/system/ringtone


请输入信息














































































































































































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

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

(0)
上一篇 2026年3月26日 下午7:45
下一篇 2026年3月26日 下午7:46


相关推荐

  • 前端节流、防抖

    前端节流、防抖1 JS 的节流 防抖及使用场景概念和例子在事件被触发 n 秒后再执行回调 如果在这 n 秒内又被触发 则重新计时 看一个 栗子 模拟一段 ajax 请求 functionajax content console log ajaxrequest content letinputa document getElementBy unDeb

    2026年3月19日
    2
  • jmeter-实现跨线程组之间传递参数

    jmeter-实现跨线程组之间传递参数

    2021年9月18日
    60
  • 码蹄集新手村100题答案「建议收藏」

    码蹄集新手村100题答案「建议收藏」码蹄集是今年新上线的一个OJ平台,内含了100道基础题和一些百度之星的题目。由于很多题目有原创性,搜不到相关解答,因此我花了两天特将100道题目刷了一遍,目前位居榜二。码蹄集传送门:https://www.matiji.net/exam/ojquestionlist前言所有题目均能AC,不一定是最佳方法,如有其它方法,可在评论区留言讨论。1、程序设计入门#include<iostream>usingnamespacestd;intmain(){co.

    2022年10月8日
    6
  • HashMap的containsKey方法说明

    HashMap的containsKey方法说明Returnstheen HashMap Returnsnulli forthekey finalEntryge Objectkey

    2026年2月11日
    2
  • 元胞自动机算法汇总含matlab代码_数学建模(十三)

    元胞自动机算法汇总含matlab代码_数学建模(十三)元胞自动机理论许多复杂的问题都可以通过元胞自动机来建立模型 元胞自动机实质上是定义在一个具有离散 有限状态的元胞组成的元胞空间上 并按照一定的局部规则 在离散的时间维度上演化的动力学系统 元胞又可称为单元 细胞 是元胞自动机的最基本的组成部分 元胞具有以下特点 1 元胞自动机最基本的单元 2 元胞有记忆贮存状态的功能 3 所有元胞状态都按照元胞规则不断更新 演化规则中心元胞的下一个

    2026年3月20日
    1
  • 协方差矩阵—Hessian矩阵—正定矩阵

    协方差矩阵—Hessian矩阵—正定矩阵一 基本概念 1 1 协方差矩阵及推导 1 2 黑塞矩阵示例 1 3 正定矩阵定义及性质 1 4 正定矩阵示例一 基本概念 1 1 协方差矩阵及推导在统计学中用标准差描述样本数据的 散布度 公式中之所以除以 n 1 而不是 n 是因为这样使我们以较少的样本集更好的逼近总体标准差 即统计学上所谓的 无偏估计 协方差矩阵的

    2025年10月17日
    5

发表回复

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

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