(十)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();
        }
    }
}
原文地址:https://www.cnblogs.com/fuyanan/p/4030739.html