(十)android 中数据存储与访问——使用SharedPreferences保存数据

10.1 SharedPreferences概述

数据存储方式有五种,前面介绍的是通过IO流以文件的方式存储数据,这里学习的SharedPreferences方式保存的数据,主要保存的是用户的偏好设置.

很多时候,我们开发的程序是需要向用户提供软件参数设置功能的。用户根据自己的兴趣爱好对软件的各项参数进行配置,以符合自己的使用习惯。

例如,我们使用eclipse的时候,可以设置字体的显示颜色、大小等。Eclipse内部使用的是xml格式的文件来保存软件的配置参数。

如果我们要在安卓中保存用户在软件上设置的各项参数,使用的方式是SharedPreferences,其内部也是使用xml文件来保存参数的,但是对于我们而言,我们不需要直接跟xml文件打交道,因为SharedPreferences已经对操作xml的文件的API进行了封装。

这种数据存储方式专门就是用于干这个事情的。

10.2 项目需求

项目需求与第九章一致,只是保存用户名和密码,使用的方式是SharedPreferences

10.3 activity_main.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/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"
    tools:context="com.example.logintest.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入用戶名"
        android:textSize="18dp" />

    <EditText
        android:id="@+id/et_username"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入密碼"
        android:textSize="18dp" />

    <EditText
        android:id="@+id/et_pwd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="numberPassword"
        android:textSize="18dp" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/cb_checked"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="记住密码"
            android:textSize="18dp" />

        <Button
            android:id="@+id/bt_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="登陆"
            android:textSize="18dp" />
    </RelativeLayout>

</LinearLayout>

10.4 MainActivity.java文件

package com.example.sharedperences;

import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    private EditText mUsername;
    private EditText mUserpassword;
    private Button mLogin;
    private SharedPreferences sp;
    private Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mUsername = (EditText) findViewById(R.id.et_username);
        mUserpassword = (EditText) findViewById(R.id.et_pwd);
        mLogin = (Button) findViewById(R.id.bt_login);
        mLogin.setOnClickListener(this);

        sp = this.getSharedPreferences("userInfo", MODE_PRIVATE);
        mUsername.setText(sp.getString("userName", null));
        mUserpassword.setText(sp.getString("userNumber", null));
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        String name = mUsername.getText().toString().trim();
        String number = mUserpassword.getText().toString().trim();

        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(number)) {
            Toast.makeText(this, "用户名或密码为空", 1).show();
            return;
        } else {
            editor = sp.edit();
            editor.putString("userName", name);
            editor.putString("userNumber", number);
            editor.commit();
        }
    }
}
时间: 2024-08-24 23:11:22

(十)android 中数据存储与访问——使用SharedPreferences保存数据的相关文章

Android下的数据存储与访问(1) --- 以文件的形式

Android下的数据存储与访问(1) --- 以文件的形式 1.1 储存文件存放在手机内存中: // *** 储存数据到 /data/data/包名/files/jxn.txt文件中 String data = "test"; // /data/data/包名/files File filesDir = context.getFilesDir(); File file = new File(filesDir, "jxn.txt"); FileOutputStrea

Android学习笔记 --- 数据存储与访问 (File,sdcard,sharedpreferences,sqlite)

一.使用文件进行数据存储 1.context.openFileOutput()方法  写入文件内容 在上下文context中 openFileOutput方法可以用于把数据输出到文件中 示例代码: public static void fileStorage(Context context){ try { FileOutputStream fos = context.openFileOutput("filedata.txt", context.MODE_PRIVATE); fos.wr

Android 测试、数据存储与访问、XML解析与生成

1.android测试 1.黑盒测试: 是以用户的角度,从输入数据与输出数据的对应关系出发进行测试的. 2. 白盒测试: 又称结构测试.透明盒测试.逻辑驱动测试或基于代码的测试. 3.单元测试: 又称模块测试,是开发者编写的一小段代码,用于检验被测代码的一个很小的.很明确的功能是否正确. 4.功能测试: 根据产品特性.操作描述和用户方案,测试一个产品的特性和可操作行为以确定它们满足设计需求. 5.压力测试: 主体向被观察者布置一定量任务和作业,借以观察个体完成任务的行为. 6.集成测试: 是单元

从零开始学android&lt;数据存储(1)SharedPreferences属性文件.三十五.&gt;

在android中有五种保存数据的方法,分别是: Shared Preferences Store private primitive data in key-value pairs. 对应属性的键值对属性文件存储 Internal Storage Store private data on the device memory. 设备内存存储 External Storage Store public data on the shared external storage. 外部存储器存储,如内

数据存储与访问之——初见SQLite数据库

      本节引言: 本节学习Android数据库存储与访问的第三种方式:SQLite数据库,和其他的SQL数据库不同,我们并不需要在手机上另外安装一个数据库手机软件,Android系统已经集成了这个数据库,我们无需像使用其他数据库软件(Oracle,MSSQL,MySql等)又要安装,然后完成相关配置,又要改端口之类的!       1.基本的概念 1)SQLite是什么?为什么要用SQLite?SQLite有什么特点? 答:下面本姑娘来为大家讲解 SQLite是一个轻量级的关系型数据库,运

Android数据存储(一)----SharedPreferences详解

一.Android数据的存储方式: Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File:此外还有一种网络存储.由于Android系统中,数据基本都是私有的,都是存放于“data/data/程序包名”目录下,所以要实现数据共享,正确方式是使用Content Provider. 在Android中,可以使用几种方式实现数据持久化: Shared Preferences:除SQLite数据库外,另一种常用的数据

数据存储和访问

1.SharedPreferences: 通过SharedPreferences可以将NVP(Name/Value Pair,名称/值对)保存在Android的文件系统中, 还能够实现不同应用程序间的数据共享. 支持三种访问mode: 私有(MODE_PRIVATE):仅有创建程序有权限对其进行读取或写入 全局读(MODE_WORLD_READABLE):不仅创建程序可以对其进行读取或写入,其他应用程序也读取操作的权限,但没有写入操作的权限 全局写(MODE_WORLD_WRITEABLE):创

Android中的安全与访问权限控制

Android是一个多进程系统,在这个系统中,应用程序(或者系统的部分)会在自己的进程中运行.系统和应用之间的安全性是通过Linux的facilities(工具,功能)在进程级别来强制实现的,比如会给应用程序分配user ID和Group ID.更细化的安全特性是通过"Permission"机制对特定的进程的特定的操作进行限制,而"per-URI permissions"可以对获取特定数据的access专门权限进行限制. 安全架构 Android安全架构中一个中心思

Android中使用http协议访问网络

HTTP协议的工作原理:客户端向服务器端发送http请求,服务器端收到请求后返回一下数据给客户端,客户端接受消息并进行解析. 在Android中发送http请求的方式有两种,第一种是通过HttpURLConnection的方式,第二种是通过HttpClient的方式. 通过HttpURLConnection的方式发送http请求 通常分为以下5个步骤: 1.获取HttpURLConnection实例对象.先new一个URL实例,然后调用该对象的openConnection()方法. 2.设置ht