安卓xml怎么查看sharedpreference xml文件

Android 代码实现preference组件 - 推酷
Android 代码实现preference组件
& & &&Preference主要实现一些配置数据,一些我们上次点击选择的内容,我们希望在下次应用调起的时候依然有效,无须用户再一次进行配置或选择。Android提供preference这个键值对的方式来处理这种情况,自动保存这些数据,并立时生效,同时Android提供一种类似的layout的方式来进行Preference的布局。
Preference组件有ListPreference,EditTextPreference,CheckBoxPreference和SwitchPreference,相对于View中的ListView,EditText,CheckBox,Switch和RingtonePreference&.
1.介绍Preference组件的相关属性
&:&&&&&&&&& 每个Preference控件独一无二的”ID”,唯一表示此Preference。&&&&&&&&&&
&: 默认值。 例如,CheckPreference的默认值可为”true”,默认为选中状态;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&EditTextPreference的默认值可为”110”。
&:&&&&& 表示该Preference是否可用状态。&&&&&
&:&&&&&&& 每个Preference在PreferenceScreen布局上显示的标题——大标题
&:&&&&& 每个Preference在PreferenceScreen布局上显示的标题——小标题(可以没有)
:&&& 表示Preference元素所对应的值是否写入sharedPreferen文件中,如果是true,则表示写
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&入;否则,则表示不写入该Preference元素的值。
:&&& 表示一个Preference(用A表示)的可用状态依赖另外一个Preference(用B表示)。B可用,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&则A可用;B不可用,则A不可用。
&&&&&&&&& android:disableDependentsState :& 与
相反。B可用,则A不可用;B不可用,则A可用。
特性:ListPreference
&&&&&&&&&&&&& android:dialogTitle:弹出控件对话框时显示的标题
&&&&&&&&&&&&&&
:类型为array,控件欲显示的文本
&&&&&&&&&&&&&&
:类型为array,与文本相对应的key-value键值对,value保存至sharedPreference文件
RingtonePreference
&&&&&&&&&&&&&
:响铃的铃声类型,主要有:ringtone(音乐)、notification(通知)、alarm(闹铃)
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&、all(所有可用声 音类型)。
&&&&&&&&&&&&&
&:默认铃声,可以使用系统(布尔值---true,false)的或者自定义的铃声
&&&&&&&&&&&&&
& :指定铃声是否为静音。指定铃声包括系统默认铃声或者自定义的铃声
2.实例实现上述所讲控件
1& 代码目录结构
1&xml相关文件
a.xml-& setting_preference.xml
Preference的xml文件不是放在res-&layout下,而是需要新建一个xml文件夹,将setting_preference.xml放置在该xml文件夹下:
&?xml version=&1.0& encoding=&utf-8&?&
&PreferenceScreen xmlns:android=&/apk/res/android& &
&PreferenceCategory android:title=&第一项& &
&ListPreference
android:defaultValue=&Green&
android:dialogTitle=&请选您需要的颜色&
android:entries=&@array/my_color_array&
android:entryValues=&@array/my_color_array&
android:key=&list_key&
android:summary=&Green&
android:title=&颜色设置& &
&/ListPreference&
&/PreferenceCategory&
&PreferenceCategory android:title=&第二项& &
&EditTextPreference
android:defaultValue=&this is editTextPreference&
android:dialogIcon=&@drawable/ic_launcher&
android:dialogTitle=&请输入&
android:key=&edit_text_key&
android:summary=&this is editTextPreference&
android:title=&文本输入框& &
&/EditTextPreference&
&/PreferenceCategory&
&PreferenceCategory android:title=&第三项& &
&CheckBoxPreference
android:key=&check_box_key&
android:defaultValue=&true&
android:title=&CheckBox& &
&/CheckBoxPreference&
&SwitchPreference
android:key=&switch_key&
android:defaultValue=&true&
android:title=&Switch& &
&/SwitchPreference&
&Preference
android:key=&preference_key&
android:title=&关于& &
&/Preference&
&/PreferenceCategory&
&PreferenceCategory android:title=&第四项& &
&RingtonePreference
android:key=&ring_tone_key&
android:title=&Ringtone&
android:summary=&默认铃声&
android:ringtoneType=&alarm&
&&/RingtonePreference&
&/PreferenceCategory&
&/PreferenceScreen& b.layout-&activity_
&LinearLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical&
android:paddingBottom=&@dimen/activity_vertical_margin&
android:paddingLeft=&@dimen/activity_horizontal_margin&
android:paddingRight=&@dimen/activity_horizontal_margin&
android:paddingTop=&@dimen/activity_vertical_margin& &
android:id=&@+id/setting_btn&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:gravity=&center&
android:text=&设置& /&
android:id=&@+id/show_setting_info_btn&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:gravity=&center&
android:text=&显示设置信息& /&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:text=&显示设置信息:&
android:textColor=&#FF0000&
android:textSize=&20sp& /&
android:id=&@+id/show_info_tv&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:textSize=&20sp& /&
&/LinearLayout& c.layout-&activity_about.xml
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical& &
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:text=&welcome to my preference&
android:textSize=&20sp&
android:gravity=&center&
&&/TextView&
&/LinearLayout& d.value-&array.xml
&?xml version=&1.0& encoding=&utf-8&?&
&resources&
&string-array name=&my_color_array&&
&item&Red&/item&
&item&Green&/item&
&item&Bule&/item&
&/string-array&
&/resources& 2&. java代码实现
a. 常量ConstantUtil.java
package com.example.
public class ConstantUtil {
public static final String LIST_KEY = &list_key&;
public static final String EDIT_TEXT_KEY = &edit_text_key&;
public static final String CHECK_BOX_KEY = &check_box_key&;
public static final String SWITCH_KEY = &switch_key&;
public static final String RING_TONE_KEY = &ring_tone_key&;
public static final String PREFERENCE_KEY = &preference_key&;
public static final String MY_PREFERENCE_KEY = &my_preference_key&;
} b. 主界面MainActivity.java
package com.example.
import android.app.A
import android.content.I
import android.content.SharedP
import android.os.B
import android.preference.PreferenceM
import android.view.M
import android.view.MenuI
import android.view.V
import android.view.View.OnClickL
import android.widget.B
import android.widget.TextV
public class MainActivity extends Activity implements OnClickListener {
private Button settingBtn, showSettingInfoB
private TextView showInfoTv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingBtn = (Button) this.findViewById(R.id.setting_btn);
showSettingInfoBtn = (Button) this
.findViewById(R.id.show_setting_info_btn);
showInfoTv = (TextView) this.findViewById(R.id.show_info_tv);
settingBtn.setOnClickListener(this);
showSettingInfoBtn.setOnClickListener(this);
public void onResume() {
super.onResume();
showSettingInfo();
* 显示设置信息
private void showSettingInfo() {
SharedPreferences share = PreferenceManager
.getDefaultSharedPreferences(this);
StringBuffer info = new StringBuffer();
String listInfo = share.getString(ConstantUtil.LIST_KEY, &Green&);
String editTextInfo = share.getString(ConstantUtil.EDIT_TEXT_KEY,
&this is editTextPreference&);
boolean checkBoxInfo = share.getBoolean(ConstantUtil.CHECK_BOX_KEY,
boolean switchInfo = share.getBoolean(ConstantUtil.SWITCH_KEY, true);
String ringtoneInfo = share.getString(ConstantUtil.RING_TONE_KEY,
&默认铃声&);
info.append(&ListInfo:&).append(listInfo).append(&\n&)
.append(&editTextInfo:&).append(editTextInfo).append(&\n&)
.append(&checkBoxInfo:&).append(checkBoxInfo).append(&\n&)
.append(&switchInfo:&).append(switchInfo).append(&\n&)
.append(&ringtoneInfo:&).append(ringtoneInfo).append(&\n&);
showInfoTv.setText(info.toString());
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return super.onOptionsItemSelected(item);
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.setting_btn:
Intent intent = new Intent(MainActivity.this, SettingActivity.class);
startActivity(intent);
case R.id.show_setting_info_btn:
showSettingInfo();
} c. 设置界面SettingActivity.java
package com.example.
import android.content.I
import android.content.SharedP
import android.content.SharedPreferences.OnSharedPreferenceChangeL
import android.os.B
import android.preference.CheckBoxP
import android.preference.EditTextP
import android.preference.ListP
import android.preference.P
import android.preference.Preference.OnPreferenceClickL
import android.preference.PreferenceA
import android.preference.RingtoneP
import android.preference.SwitchP
import android.widget.T
public class SettingActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener,OnPreferenceClickListener{
private ListPreference mListP
private EditTextPreference mEditTextP
private CheckBoxPreference mCheckBoxP
private SwitchPreference mSwitchP
private Preference mP
private RingtonePreference mRingtoneP
@SuppressWarnings(&deprecation&)
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.addPreferencesFromResource(R.xml.setting_preference);
initPreference();
@SuppressWarnings(&deprecation&)
private void initPreference(){
mListPreference = (ListPreference) this.findPreference(ConstantUtil.LIST_KEY);
mEditTextPreference = (EditTextPreference) this.findPreference(ConstantUtil.EDIT_TEXT_KEY);
mCheckBoxPreference = (CheckBoxPreference) this.findPreference(ConstantUtil.CHECK_BOX_KEY);
mSwitchPreference = (SwitchPreference) this.findPreference(ConstantUtil.SWITCH_KEY);
mPreference = (Preference) this.findPreference(ConstantUtil.PREFERENCE_KEY);
mRingtonePreference = (RingtonePreference) this.findPreference(ConstantUtil.RING_TONE_KEY);
mPreference.setOnPreferenceClickListener(this);
@SuppressWarnings(&deprecation&)
public void onResume(){
super.onResume();
SharedPreferences share = this.getPreferenceScreen().getSharedPreferences();
setDefaultSummary(share);
share.registerOnSharedPreferenceChangeListener(this);
@SuppressWarnings(&deprecation&)
public void onPause(){
super.onPause();
this.getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
* 设置组件小标题
* @param share
private void setDefaultSummary(SharedPreferences share){
mListPreference.setSummary(share.getString(ConstantUtil.LIST_KEY, &Green&));
mEditTextPreference.setSummary(share.getString(ConstantUtil.EDIT_TEXT_KEY, &this is editTextPreference&));
mRingtonePreference.setSummary(share.getString(ConstantUtil.RING_TONE_KEY, &默认铃声&));
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
if(ConstantUtil.LIST_KEY.equals(key)){
mListPreference.setSummary(mListPreference.getEntry());
}else if(ConstantUtil.EDIT_TEXT_KEY.equals(key)){
mEditTextPreference.setSummary(sharedPreferences.getString(key, &this is editTextPreference&));
}else if(ConstantUtil.RING_TONE_KEY.equals(key)){
mRingtonePreference.setSummary(sharedPreferences.getString(key, &默认铃声&));
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
if(ConstantUtil.PREFERENCE_KEY.equals(preference.getKey())){
Intent intent = new Intent(SettingActivity.this,AboutActivity.class);
startActivity(intent);
}else if(ConstantUtil.MY_PREFERENCE_KEY.equals(preference.getKey())){
Toast.makeText(this, &点击MyPreference&, Toast.LENGTH_SHORT).show();
} d.跳转界面AboutActivity.java
package com.example.
import android.app.A
import android.os.B
public class AboutActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
} 3&. 清单AndroidManifest.xml
&?xml version=&1.0& encoding=&utf-8&?&
&manifest xmlns:android=&/apk/res/android&
package=&com.example.settingdemo&
android:versionCode=&1&
android:versionName=&1.0& &
android:minSdkVersion=&8&
android:targetSdkVersion=&19& /&
&application
android:allowBackup=&true&
android:icon=&@drawable/ic_launcher&
android:label=&@string/app_name&
android:theme=&@style/AppTheme& &
android:name=&com.example.settingdemo.MainActivity&
android:label=&@string/app_name& &
&intent-filter&
&action android:name=&android.intent.action.MAIN& /&
&category android:name=&android.intent.category.LAUNCHER& /&
&/intent-filter&
&/activity&
&activity android:name=&com.example.settingdemo.SettingActivity&&&/activity&
&activity android:name=&com.example.settingdemo.AboutActivity&&&/activity&
&/application&
&/manifest& 至此,本文已完成,大家可以试试。
源代码下载链接:
已发表评论数()
&&登&&&录&&
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见1819人阅读
在Android中一共提供了4种数据存储方式,但是由于存储的这些数据都是其应用程序私有的,所以如果需要在其他应用程序中使用这些数据,就要使用Android提供的Content Provider(数据共享)。
官方文档中有详细的描述:
Android中4种数据存储方式分别如下。
1、Shared Preferences:用来存储“Key-value paires”格式的数据。它是一个轻量级的键值存储机制,只可以存储基本数据类型。
2、Files:它通过fileInputStream和FileOutputStream对文件进行操作。但是在Android中,文件是一个应用程序私有的,一个应用程序无法读写其他应用程序的文件。
3、SQLite:Android提供的一个标准的数据库,支持SQL语句
4、Network:通过网络来存储和获得数据
一、Shared Preferences数据存储
Shared Preferences主要是针对系统配置信息的保存,比如给程序界面设置了音效,想在下一次启动时还能够保留上次设置的音效。由于Android系统的界面是采用Activity栈的形式,所以在系统资源不足时会收回一些界面,因此,有些操作需要在不活动时保留下来,以便等再次激活时能够显示出来。
SharedPreference只允许存储基本数据类型。
获取Sharedpreference对象有两种方式,有Context的实例,比如Activity,调用下面的方法:
getSharedPreference():用这种方式获得Sharedpreference对象,需要在该方法的第一个参数指定该preference文件的名字
getPreference();用这种方式可以为你的Activity获取一个唯一的preference文件,并且不用指定名字。
并且在通过这两种方式获得Sharedpreference对象时,还要指定preference文件的读取模式,有3种模式(不是4种):
public static final int MODE_PRIVATE = 0x0000;& 代表只有该程序可以访问
public static final int MODE_WORLD_READABLE = 0x0001; 代表其他程序只能进行读操作
public static final int MODE_WORLD_WRITEABLE = 0x0002; 代表其他程序可以进行读写操作
往preference文件中写数据:
1、调用edit()方法,获取一个SharedPreference.Editor对象
2、调用putBoolean()、putString()等等put...()方法存储数据
3、调用commit()方法,提交数据。因为调用put...()方法, 会先将数据存储在内存中,在调用commit()方法,将数据存储到preference文件中,这样做的好处是不必没存衣服咯数据,就对文件操作一次,节省内存开销。
从preference文件中读数据:
调用SharedPreference对象的 getBoolean、getString()等等get...()方法读取数据。
public class Calc extends Activity {
public static final String PREFS_NAME = &MyPrefsFile&;
protected void onCreate(Bundle state){
super.onCreate(state);
// 读入数据 Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
boolean silent = settings.getBoolean(&silentMode&, false);
setSilent(silent);
protected void onStop(){
super.onStop();
// 存储数据 We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(&silentMode&, mSilentMode);
// 不要忘记提交commit()
我们知道,SharedPreference 是以键值对存储的,从get和put的方式就可以看到,其实preference文件本身就是一个xml文件,打开看看就知道了。
这个用这种方式生成的preference文件,都有一个固定的位置:
File Explorer / data / data / 包名 / shared_prefs /& 文件名字.xml
调用其他应用程序的Sharedpreference文件
要读取其他应用程序的,那么那个其他的应用程序的preference文件必须是可以读或者可以读写的。
1、需要创建其他应用程序对应的Context,例如
Context context=createPackageContext(&com.tao.androidtest&, Context.CONTEXT_IGNORE_SECURITY);
第一个参数是其他应用许的包名--实际上Android系统是用应用程序的包名来作为该程序的标志的。
第二个参数为flags 标志位,有CONTEXT_INCLUDE_CODE和CONTEXT_IGNORE_SECURITY两个选项。CONTEXT_INCLUDE_CODE的意思是包括代码,也就是说可以执行这个包里面的代码。CONTEXT_IGNORE_SECURITY的意思是忽略安全警告,如果不加这个标志的话,有些功能是用不了的,会出现安全警告。
2、在用其他应用程序的Context的getSahredPreference()方法即可获取相应的SahredPreference对象。
只有那个其他应用程序的preference文件的Mode设置为MODE_WORLD_READABLE或或者MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE,才能被外部应用程序访问。
然后用法就可在本程序中使用SahredPreference对象一样的使用。
当然上面的是通过Android提供的API操作的,也可以自己构建File文件,在解析里面的xml文件在操作,只不过这种方式更为繁琐。
File file=new File(&data/data/com.tao.androidtest/shard_prefs/test.xml&);
获得File文件,然后去解析里面的xml文件,关于如何解析xml文件,以后在学习的时候介绍。
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:708035次
积分:8362
积分:8362
排名:第1063名
原创:113篇
转载:72篇
评论:236条
记录自己进步的点点滴滴,让懒惰的我能够一直的走下去。。。
(1)(1)(1)(2)(3)(3)(1)(1)(1)(1)(1)(8)(16)(4)(11)(26)(18)(23)(54)(3)(7)PreferenceScreen 偏好显示类 的使用 - 如果爱,请深爱! - ITeye技术网站
PreferenceScreen preference是偏好,首选的意思,PreferenceScreen个人翻译成 “偏好显示”,明白这个意思就好,就是说根据特点灵活的定义显示内容风格,一个屏幕可以包含多个风格,像系统自带的声音设置界面。
实现这种显示效果其实很简单,只需要借助PreferenceScreen类即可。在项目资源文件中新建xml文件夹,在里面新建preferences.xml文件.
根元素为PreferenceScreen 代表显示一整个屏幕,内部嵌套PreferenceCategory标签,表示偏好类别,在PreferenceCategory标签内部可以随便存放复选框,输入框,列表等显示控件.可包含的控件内容在android.preference包下可查阅.xml文件编写好后,需要加载到activity中,对于偏好显示的xml加载,可以使用PreferenceActivity中的addPreferencesFromResource(),所以Activity需要继承PreferenceActivity.既然显示的屏幕中包含复选框,输入框的控件,我们必然对选择与否,输入框内容感兴趣,我们要怎样获取屏幕的内容呢?
复写activity中的onPreferenceTreeClick 方法,在对屏幕显示的内容进行操作时,会触发此方法,在方法内部通过调用
SharedPreferences contentPreference = preference.getSharedPreferences();
boolean checkbox_toggle = contentPreference.getBoolean("checkbox_preference", false);
String animalName = contentPreference.getString("edittext_preference", "default");
即可得到屏幕上输入的内容,与使用SharedPreference对象一样,通过key-value 的形式获取,其中key是xml控件标签中key属性对应的值.
显示效果:
对应xml文件:
&?xml version="1.0" encoding="utf-8"?&
&PreferenceScreen
xmlns:android="/apk/res/android"&
&PreferenceCategory
android:title="显示一排偏好"&
&CheckBoxPreference
android:key="checkbox_preference"
android:title="开关偏好"
android:summary="这是一个开关按钮" /&
&/PreferenceCategory&
&PreferenceCategory
android:title="基于对话框的偏好"&
&EditTextPreference
android:key="edittext_preference"
android:title="文本输入偏好"
android:summary="使用一个文本框对话框"
android:dialogTitle="输入你的宠物" /&
&ListPreference
android:key="list_preference"
android:title="列表偏好"
android:summary="使用一个列表对话框"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
android:dialogTitle="选择一个" /&
&/PreferenceCategory&
&PreferenceCategory
android:title="启动偏好"&
&PreferenceScreen
android:key="screen_preference"
android:title="屏幕"
android:summary="显示另一个偏好屏幕"&
&!-- You can place more preferences here that will be shown on the next screen. --&
&CheckBoxPreference
android:key="next_screen_checkbox_preference"
android:title="开关偏好"
android:summary="另一个屏幕上的偏好" /&
&/PreferenceScreen&
&PreferenceScreen
android:title="意图偏好"
android:summary="通过意图启动一个Activity"&
&intent android:action="android.intent.action.VIEW"
android:data="" /&
&/PreferenceScreen&
&/PreferenceCategory&
&PreferenceCategory
android:title="偏好属性"&
&CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="父开关"
android:summary="这是一个父开关" /&
&CheckBoxPreference
android:key="child_checkbox_preference"
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
android:title="子开关"
android:summary="这是一个子开关" /&
&/PreferenceCategory&
&/PreferenceScreen&
浏览 11194
浏览: 167969 次
来自: 北京
这个怎么用啊?
今天面了3家,真心和LZ一样的感受
可以多个层次吗
现在是两层次
LoveZhou 写道mvpstevenlin 写道我在模拟器 ...
Button的数据一多还是会覆盖啊

我要回帖

更多关于 安卓xml解析 的文章

 

随机推荐