在此之前的学习内容是数据存储之一文件存储。在本地存储中常用的有,文件、配置文件、数据库。前面的学习主要是针对本地文件的。我认为可以把SharedPreferences看做是配置文件,虽然它也是采用XML格式存储的。

比如我们使用的桌面软件中,通常会有一个“选项”菜单,选项是对软件的常规或核心设置。在Android中我们使用SharedPreferences来完成这种对配置文件的读写。在JavaSE和JavaEE中常用的是*.properties,在Windows平台下常使用*.ini文件。

下面,我们编写一个使用SharedPreferences读写配置文件的小例子。

1.创建Android工程

Project name:AndroidSharedPreferences

BuildTarget:Android2.1

Application name:Android 应用程序配置

Package name:com.changcheng.sharedpreferences

Create Activity:AndroidSharedPreferences

Min SDK Version:7

2.编辑strings.xml:

<!--l version=<-->"1.0" encoding="utf-8"?>

"hello">Hello World, AndroidSharedPreferences!

"app_name">Android 应用程序配置

"tv_name">姓名

"tv_age">年龄

"bt_write">设置

"bt_read">读取

"save_success">保存成功

"save_failed">保存失败

3.编辑main.xml

<!--l version=<-->"1.0" encoding="utf-8"?>

"http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

<!-- 姓名 -->

"fill_parent"

android:layout_height="wrap_content">

"70dip" android:layout_height="wrap_content"

android:textSize="25dip" android:id="@+id/tv_name" android:text="@string/tv_name" />

"300dip"

android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_name"

android:id="@+id/et_name" />

<!-- 年龄 -->

"fill_parent"

android:layout_height="wrap_content">

"70dip" android:layout_height="wrap_content"

android:textSize="25dip" android:id="@+id/tv_age" android:text="@string/tv_age" />

"300dip"

android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_age"

android:id="@+id/et_age" />

<!-- 按钮 -->

"fill_parent"

android:layout_height="wrap_content" android:gravity="right">

"wrap_content"

android:layout_height="wrap_content" android:text="@string/bt_write"

android:id="@+id/bt_set" />

"wrap_content"

android:layout_height="wrap_content" android:layout_toRightOf="@id/bt_set"

android:text="@string/bt_read" android:id="@+id/et_read" />

4.为按钮添加事件代码

package com.changcheng.sharedpreferences;

import android.app.Activity;

import android.content.Context;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class AndroidSharedPreferences extends Activity {

private static final String TAG = "AndroidSharedPreferences";

private EditText etName;

private EditText etAge;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// 获取按钮

Button btSet = (Button) this.findViewById(R.id.bt_set);

Button btRead = (Button) this.findViewById(R.id.bt_read);

// 获取编辑框

etName = (EditText) this.findViewById(R.id.et_name);

etAge = (EditText) this.findViewById(R.id.et_age);

// 添加事件

btSet.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// 获取名称和年龄

String name = etName.getText().toString();

String age = etAge.getText().toString();

// 创建SharedPreferences

SharedPreferences sp = getSharedPreferences("preferences",

Context.MODE_PRIVATE);

// 添加数据

Editor editor = sp.edit();

editor.putString("name", name);

editor.putInt("age", Integer.parseInt(age));

// 保存数据

if (editor.commit())

Toast.makeText(AndroidSharedPreferences.this,

R.string.save_success, 1).show();

else

Toast.makeText(AndroidSharedPreferences.this,

R.string.save_failed, 1).show();

}

});

btRead.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// 创建SharedPreferences

SharedPreferences sp = getSharedPreferences("preferences",

Context.MODE_PRIVATE);

// 获取数据

String name = sp.getString("name", "defName");

String age = sp.getInt("age", 0) + "";

// 显示数据

etName.setText(name);

etAge.setText(age);

}

});

}

}

5.运行程序

启动模拟器,运行程序。输入名称和年龄,点击保存。我们使用的代码是getSharedPreferences("preferences",Context.MODE_PRIVATE);,当然commit时。它会为我们为”/data/data/com.changcheng.sharedpreferences/shared_prefs/preferences.xml”。将 preferences.xml导出,查看它的内容为:

<!--l version='1.0' encoding='utf-8' standalone='yes'-->

长城

将名称和年龄编辑框的内容清空,然后点击读取按钮,刚才写出的内容被读取进来。 SharedPreferences的使用就是这么简单。

6.其他程序访问本程序的配置

通过SharedPreferences创建的配置文件,不需要指定路径和文件后缀名,读取的时候也是。通常情况下,配置只是提供给本应用程序使用的。在这里我们介绍一个小知识点,即其他程序想使用本应用程序的配置,那应该如何使用SharedPreferences呢?如下:

Context otherAppContext = createPackageContext("com.changcheng.sharedpreferences", Context.CONTEXT_IGNORE_SECURITY);

SharedPreferences sharedPreferences = otherAppContext.getSharedPreferences("preferences", Context.MODE_WORLD_READABLE);

注意,为了使其他程序可以访问本应用程序的配置。那么在我们使用 getSharedPreferences创建配置的时候必须为它的文件访问模式设置为允许其他程序读取或写入等。

更多相关文章

  1. Google Maps Android(安卓)API v2 (3)- 地图添加到Android应用程
  2. 【Android(安卓)UI设计与开发】第05期:引导界面(五)实现应用程序只
  3. Android(安卓)Studio安卓学习笔记(一)安卓与Android(安卓)Studio
  4. 我的第一个应用程序:如何逐步创建第一个Android应用程序
  5. 虚拟机运行 Android(安卓)程序背后的故事
  6. Android的界面设计规范-5
  7. Android(安卓)4 高级编程(第3版)试读
  8. Android(安卓)Activity 和 Task 设计指导
  9. Android中库项目、jar包等的使用方法

随机推荐

  1. Android(安卓)Spinner
  2. 一步步教你用Android(安卓)Google Map
  3. GDB + gdbserver 远程调试android native
  4. 在Ubuntu上下载、编译和安装Android最新
  5. android中Content Provider
  6. Android(安卓)LinearLayout的布局属性介
  7. android
  8. android 获取路径目录方法以及判断目录是
  9. android 数字键盘使用
  10. android中从图库中选取图片