一.SharedPreferences是什么
存储一些轻量级的数据,以键值对形式存储数据
SharedPreferences轻量级的存储类
二.如何存储数据
1.存储数据
实例化SharedPreferences对象
第一个参数是自己命的名(key键)
注意:key键是惟一的,给同名键赋值是会被覆盖
第二个参数代表数据的操作模式
SharedPreferences的四种操作模式:
Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE
- Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.- Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
- MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
- MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入.
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
实例化SharedPreferences.Editor对象
SharedPreferences.Editor editor = sharedPreferences.edit();
用putString的方法保存数据
editor.putInt("check", 1);//记录保存标记
editor.putString("username", name_et.getText().toString());//记录用户名
editor.putString("password", pw_et.getText().toString());//记录密码
注意:提交当前数据
editor.commit();
存储完后可以在Tools/Android/Android Device Monitior中找到
注意:这里或许会ADB异常
只需要
点击Enable ADB
再重建项目运行
2.获取数据
同样,在读取SharedPreferences数据前要实例化出一个SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
使用getString方法获得value,注意第2个参数是value的默认值
String name = sharedPreferences.getString("username", "");
String pw = sharedPreferences.getString("password", "");
三.案例实现记住密码
UI
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.a22120.day6chucunactivity.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名:"
android:id="@+id/lay_name"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密 码:"
android:id="@+id/lay_pass"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 保存密码"
android:id="@+id/checkbox_bacun"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lay_btnlogin"
android:text="登录"
/>
</LinearLayout>
</LinearLayout>
MainActivity
package com.example.a22120.day6chucunactivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
EditText tv_name,tv_passworld;
Button btn_login;
CheckBox cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindId();
SharedPreferences sharedPreferences=getSharedPreferences("sp",MODE_PRIVATE);
int checked=sharedPreferences.getInt("checked",0);
if(checked==1){
String name=sharedPreferences.getString("name","");
String passworld=sharedPreferences.getString("passworld","");
tv_name.setText(name);
tv_passworld.setText(passworld);
cb.setChecked(true);
}else{
cb.setChecked(false);
}
}
private void bindId() {
tv_name=findViewById(R.id.lay_name);
tv_passworld=findViewById(R.id.lay_pass);
btn_login=findViewById(R.id.lay_btnlogin);
cb=findViewById(R.id.checkbox_bacun);
btn_login.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.lay_btnlogin:
SharedPreferences sharedPreferences=getSharedPreferences("sp",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
if (cb.isChecked()){
String username=tv_name.getText().toString();
String passworld=tv_passworld.getText().toString();
editor.putString("name",username);
editor.putString("passworld",passworld);
editor.putInt("checked",1);
}else {
String username=tv_name.getText().toString();
String passworld=tv_passworld.getText().toString();
editor.putString("name","");
editor.putString("passworld","");
editor.putInt("checked",0);
}
editor.commit(); //提交数据
break;
}
}
}