一.SharedPreferences是什么
存储一些轻量级的数据,以键值对形式存储数据
SharedPreferences轻量级的存储类
二.如何存储数据
实例化SharedPreferences对象
第一个参数是自己命的名
第二个参数代表私有数据,只能被应用本身访问
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();
三.如何读取数据
同样,在读取SharedPreferences数据前要实例化出一个SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
使用getString方法获得value,注意第2个参数是value的默认值
String name = sharedPreferences.getString("username", "");
String pw = sharedPreferences.getString("password", "");
四.记住密码案例
输入用户名和密码,选择记住密码,等下次登录的时候不用再输入用户名和密码,我这里写的比较简单没有判断用户和密码是否正确
布局文件:
<?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"
android:orientation="vertical"
tools:context="com.example.myapplication.SharedPreferencesActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="用户:"
android:textSize="20sp" />
<EditText
android:id="@+id/name_et"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="密码:"
android:textSize="20sp" />
<EditText
android:id="@+id/pw_et"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/remember_cb"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="记住密码" />
<CheckBox
android:id="@+id/zdlogin_cb"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="自动登入" />
<CheckBox
android:id="@+id/forget_cb"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="忘记密码" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/login_btn"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="登录" />
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="注册" />
</LinearLayout>
</LinearLayout>
效果图:
activity类:
给登录按钮添加一个点击事件,并把数据存储进去
public void onClick(View view) {
switch (view.getId()) {
case R.id.login_btn:
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (remember_cb.isChecked()) {
editor.putInt("check", 1);//记录保存标记
editor.putString("username", name_et.getText().toString());//记录用户名
editor.putString("password", pw_et.getText().toString());//记录密码
} else {
editor.putString("username", "");
editor.putString("password", "");
editor.putInt("check", 0);
}
editor.commit();
Intent intent = new Intent(SharedPreferencesActivity.this, ProgressActivity.class);
startActivity(intent);
break;
}
}
我设置了一个判断是否记住密码的一个标志editor.putInt(“check”, 0);或editor.putInt(“check”, 1);
如果为0就是记住密码,如果为1就是直接从本地读取保存的数据
if (check == 1) {
String name = sharedPreferences.getString("username", "");
String pw = sharedPreferences.getString("password", "");
name_et.setText(name);
pw_et.setText(pw);
editor.commit();
remember_cb.setChecked(true);
} else {
remember_cb.setChecked(false);
}
下面是完整代码:
这就是SharedPreferences存储数据和读取数据的运用
package com.example.myapplication;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.PagerAdapter;
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 SharedPreferencesActivity extends AppCompatActivity implements View.OnClickListener {
private TextView sp_tv;
private EditText name_et, pw_et;
private Button login_btn;
private CheckBox remember_cb, zdlogin_cb, forget_cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
bindID();
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
int check = sharedPreferences.getInt("check", 0);
if (check == 1) {
String name = sharedPreferences.getString("username", "");
String pw = sharedPreferences.getString("password", "");
name_et.setText(name);
pw_et.setText(pw);
editor.commit();
remember_cb.setChecked(true);
} else {
remember_cb.setChecked(false);
}
}
private void bindID() {
name_et = findViewById(R.id.name_et);
pw_et = findViewById(R.id.pw_et);
remember_cb = findViewById(R.id.remember_cb);
forget_cb = findViewById(R.id.forget_cb);
zdlogin_cb = findViewById(R.id.forget_cb);
login_btn = findViewById(R.id.login_btn);
login_btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.login_btn:
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (remember_cb.isChecked()) {
editor.putInt("check", 1);//记录保存标记
editor.putString("username", name_et.getText().toString());//记录用户名
editor.putString("password", pw_et.getText().toString());//记录密码
} else {
editor.putString("username", "");
editor.putString("password", "");
editor.putInt("check", 0);
}
editor.commit();
Intent intent = new Intent(SharedPreferencesActivity.this, ProgressActivity.class);
startActivity(intent);
break;
}
}
}