一,SharedPreferences基础知识:
SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出。
SharedPreferences提供了java常规的Long、Int、String等类型数据的保存接口。 SharedPreferences类似过去Windows系统上的ini配置文件,但是它分为多种权限,可以全局共享访问。
提示最终是以xml方式来保存,整体效率来看不是特别的高,对于常规的轻量级而言比SQLite要好不少,如果真的存储量不大可以考虑自己定义文件格式。xml处理时Dalvik会通过自带底层的本地XML Parser解析,比如XMLpull方式,这样对于内存资源占用比较好。
二,用SharedPreferences实现简单数据存储:
(1)看下效果:图一(填写用户名并记住),图二(再次打开时:)
(2)具体实现:
对于案例中的xml文件和id的绑定就不用说了,我们还是把重点放在SharedPreferences吧!
①获取SharedPreferences对象:
SharedPreferences spf=getSharedPreference("文件名",int)
//第一个参数:想把数据保存在那个文件的文件名
//第二个参数:权限设置 :MODE_PRIVATE(私有型)
//定义一个SharedPreferences对象
SharedPreferences spf;
//第一个参数:将数据保存的文件名
//第一个参数:权限设置
// MODE_PRIVATE //私有
spf=getSharedPreferences("filename",MODE_PRIVATE);
②获取SharedPreferences.Editor对象,进行数据的存入;
//获取SharedPreferences.Editor对象
SharedPreferences.Editor editor=spf.edit();
③通过editor的putXxx(putString(),putInt()...)方法保存数据 //存一个key为username的数据
editor.putString("username",username.getText().toString());
④editor.commit()方法提交:(必须提交,不然数据就存不了); //存入后必须提交
editor.commit();
⑤获取数据://去获取数据,如果没有,设置为空
String name= spf.getString("username","");
username.setText(name);
三:原码:
package com.maeeage.administrator.androidlearn;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by Administrator on 2018/2/1/001.
*/
public class Login extends Activity {
EditText username,password;
CheckBox box;
Button btn_login;
// //定义一个SharedPreferences对象
SharedPreferences spf;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
findById();
init();
}
private void findById() {
username=(EditText)findViewById(R.id.username);
password=(EditText)findViewById(R.id.password);
box=(CheckBox)findViewById(R.id.checkBox);
btn_login=(Button)findViewById(R.id.btn_login);
}
private void init() {
//第一个参数:将数据保存的文件名
//第一个参数:权限设置
// MODE_PRIVATE //私有
spf=getSharedPreferences("filename",MODE_PRIVATE);
//去获取数据,如果没有,设置为空
String name= spf.getString("username","");
username.setText(name);
//默认选择记住用户名
box.setChecked(true);
//btn_login点击事件
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(box.isChecked())
{
//获取SharedPreferences.Editor对象
SharedPreferences.Editor editor=spf.edit();
//存一个key为username的数据
editor.putString("username",username.getText().toString());
//存入后必须提交
editor.commit();
}else{
SharedPreferences.Editor editor=spf.edit();
editor.putString("username","");
editor.commit();
}
if("admin".equals(username.getText().toString())&&"123456".equals(password.getText().toString()))
Toast.makeText(Login.this,"登录成功",Toast.LENGTH_SHORT);
else
Toast.makeText(Login.this,"登录失败",Toast.LENGTH_SHORT);
}
});
}
}
四,总结
SharedPreferences在Android开发中四很重要,认真学习和多多练习。