Android数据存储方式(一)SharedPerfences

      SharedPreferences,是android提供用来存储一些简单的配置信息的一种机制。以键值对的形式存储基本的数据类型。(boolean,int,float,long,String).

      应用的地方:应用程序,”设置“,”首选项“界面的保存;登陆用户名和密码;保存上一次用户的修改;自定义参数的设置。

      注意: 一个应用程序的配置文件只能在本应用程序中使用,只能在同一个包,不能在不同的包使用。

        1根据文件名获取指定的SharePrefrerences对象

            Context.getSharedPreferences(String name,int mode)

            参数说明:name:为Prefercences的文件名字。若不存在,在提交数据时自动创建Prefercences。处在同一应用程序的其他组件可以通过指定文件名字共享Prefercences。

                          mode:指定文件的操作模式。

                                   Context.MODE_PRIVATE 只能由应用程序调用,默认”0“。

                                   Context.MODE_WORLD_READABLE 允许所有应用程序有读取文件的权限,默认”1“。

                                   Context.MODE_PRIVATE_WRITEABLE 允许所有应用程序有写入文件的权限,默认”2“。

     SharePreferences保存数据的步骤:

        1获取对象。通过SharePreferences接口的edit获得SharePreferences.Editor的对象。

        2保存信息。通过对象SharePreferences.Editor的接口putXXX方法保存Key-value.

        3提交信息。通过SharePreferences.Editor接口的commit方法保存Key-value。

   查看结果:

       SharedPreferences采用XML格式将数据存储到设备中。保存在DDMS的File Explorer中的/data/data<包名>/shares_prefs下。

       

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">



    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <TextView android:text="用户名:" android:id="@+id/TextView01"
            android:layout_weight="3" android:layout_width="wrap_content"
            android:layout_height="wrap_content"></TextView>
        <EditText android:text="" android:id="@+id/nameET"
            android:layout_weight="7" android:layout_width="wrap_content"
            android:layout_height="wrap_content"></EditText>
    </LinearLayout>

    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <TextView android:text="密 码:" android:id="@+id/TextView01"
            android:layout_weight="3" android:layout_width="wrap_content"
            android:layout_height="wrap_content"></TextView>
        <EditText android:text="" android:id="@+id/passwordET"
            android:password="true" android:layout_weight="7"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
    </LinearLayout>

    <LinearLayout android:id="@+id/LinearLayout03"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <TextView android:text="" android:id="@+id/TextView01"
            android:layout_weight="3" android:layout_width="wrap_content"
            android:layout_height="wrap_content"></TextView>
        <Button android:id="@+id/loginBtn" android:layout_width="wrap_content"
            android:text="登陆" android:layout_height="wrap_content"></Button>
        <Button android:text="退出" android:id="@+id/stopBtn"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
</LinearLayout>

       

package com.dream;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SharedPreferencesTestActivity extends Activity implements android.view.View.OnClickListener {
    /** Called when the activity is first created. */
    //定义变量接收四个控件
    private EditText nameET = null;
    private EditText passwordET = null;
    private Button loginBtn = null;
    private Button stopBtn = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获取四个控件的ID
        nameET=(EditText) findViewById(R.id.nameET);
        passwordET =(EditText) findViewById(R.id.passwordET);
        loginBtn =(Button)findViewById(R.id.loginBtn);
        stopBtn =(Button)findViewById(R.id.stopBtn);
      //在activity创建时候从Prefercens恢复登陆信息到EditText。
        //获取Activity的Preferences对象
        SharedPreferences sp = getPreferences(MODE_PRIVATE);
        //从Preferences对象中获取登陆信息并显示到EditText中
        nameET.setText(sp.getString("loginName", ""));
        passwordET.setText(sp.getString("password", ""));
         //设置监听
        loginBtn.setOnClickListener(this);
        stopBtn.setOnClickListener(this);
        
    }
   @Override
    protected void onStop(){
        //接受EidtText的值
        String name = nameET.getText().toString();
        String password = passwordET.getText().toString();
        //定义SharedPreferences.Editor接口对象
        SharedPreferences.Editor edit = getPreferences(0).edit();
        //吧登陆信息保存到preferences中
        edit.putString("loginName", name);
        edit.putString("password", password);
        //提交信息
        edit.commit();
        super.onStop();
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //判断传过来的监听事件。
        switch(v.getId()){
        case R.id.loginBtn:
            Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show();
            break;
        case R.id.stopBtn:
finish();
break; default: break; } } }

    遇到的问题:空指针异常。没有获取ID就。定义了Preferences的对象。造成了空指针异常。

原文地址:https://www.cnblogs.com/LuckStarShine/p/2579582.html