Android进阶之路 - 四大存储之SharedPreferences存储

来源:互联网 发布:河北邢台招聘程序员 编辑:程序博客网 时间:2024/06/08 08:19

上篇给大家讲解的是文件的存储,那么本篇为大家呈现的就是简单易学的SharedPreferences的存储与数据读取

文件存储文章地址:http://blog.csdn.net/qq_20451879/article/details/54973664

sp与文件存储的不同点:
文件存储是以文本的形式存储,存的内容更多的是一整块内容,很难分化,而sp是以键值对的形式存储信息,所以扩展性更强,但是因为sp的存储空间并不大,虽然存储方便,但只是存一些细小的精确数据。

注意:
1.首先我们确认是存还是取,当然都会通过getSharedPreferences的方法,我们需要确认存储时候使用的文件名称与存储方式,存的时候我们因为是输入,所以会返回Editor,取的时候则不需要
2.SharedPreferences因为是键值对的存在,所以不论存取我们都需要对应处理,如我们之前没有存储部分键,则会返回我们设置的默认值
3.添加读写权限

    <uses-permission android:name="android.permission.READ_PROFILE"/>    <uses-permission android:name="android.permission.WRITE_PROFILE"/>

Start UI:
这里写图片描述

MainActivity Code:

package com.example.spstorage;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener {    private TextView mContent;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        mContent = (TextView) findViewById(R.id.main_content);        Button mSave = (Button) findViewById(R.id.save_one);        Button mLoad = (Button) findViewById(R.id.load_one);        mSave.setOnClickListener(this);        mLoad.setOnClickListener(this);         }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.save_one:            onsave();            break;        case R.id.load_one:            onload();            break;        default:            break;        }    }    //数据保存在sp中    private void onsave() {        //带有Editor类型的均为输入,也就是存储        Editor editor = MainActivity.this.getSharedPreferences("sp_data", MODE_PRIVATE).edit();        editor.putString("name", "Jack");        editor.putInt("age", 18);        //这里要记得提交,不然数据没保存进去        editor.apply();    }    //读取SP内的信息    private void onload() {        //像这样不是Editor类型的就是读取数据了,通过键获取值        SharedPreferences spData = getSharedPreferences("sp_data", MODE_PRIVATE);        //通过键值对获取我们刚才存储的信息        String name = spData.getString("name", "");        int age = spData.getInt("age", 0);        mContent.setText("我们存储在sp的姓名:"+name+"-----存储的年龄:"+age);    }}

MainActivity Xml Code:

<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" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <EditText            android:id="@+id/main_edit"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:hint="在此输入要保存的信息"             />        <Button            android:id="@+id/save_one"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="SP保存" />    </LinearLayout>    <Button         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/load_one"        android:text="读取sp的数据"        android:gravity="center"        />    <TextView        android:id="@+id/main_content"        android:text="读取数据展示区"        android:gravity="center"        android:layout_weight="1"        android:layout_width="match_parent"        android:layout_height="0dp" /></LinearLayout>

End UI:

这里写图片描述

我们发现存储的数据及为代码中:

这里写图片描述

我们可以通过DDMS查看数据保存结果:

这里写图片描述

导出后的数据展示:

这里写图片描述

0 0
原创粉丝点击