Android学习日记 Sharepreferences下的记住密码功能
一、Sharepreferences的作用
SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出。
二、使用步骤
Shareprefrences实现记住密码功能
代码如下:
MainActivity.java
package com.example.practice;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText userEdit,pwdEdit;
private Button btn;
private CheckBox checkBox ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
userEdit = findViewById(R.id.userEdit);
pwdEdit = findViewById(R.id.pwdEdit);
checkBox = findViewById(R.id.checkbox);
SharedPreferences sharedPreferences = getSharedPreferences("data",Context.MODE_PRIVATE);
int ischeck = sharedPreferences.getInt("checked",2);
if (ischeck==1){
checkBox.setChecked(true);
userEdit.setText(sharedPreferences.getString("user",""));
pwdEdit.setText(sharedPreferences.getString("pwd",""));
}else{
checkBox.setChecked(false);
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkBox.isChecked()){
Log.e("aaa", "123456");
SharedPreferences sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("checked",1);
editor.putString("user",userEdit.getText().toString());
editor.putString("pwd",pwdEdit.getText().toString());
editor.commit();
}else{
Log.e("aaa", "false");
SharedPreferences sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.putInt("checked",2);
editor.commit();
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="@color/black"
android:textSize="30dp"
/>
<EditText
android:id="@+id/userEdit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
/>
<EditText
android:id="@+id/pwdEdit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:text="记住密码"
/>
<Button
android:id="@+id/btn"
android:text="登录"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"/>
</LinearLayout>
</LinearLayout>
核心代码
本地存储数据
//SharedPreferences接口主要负责读取Preferences数据
// getSharedPreferences(【文件名】, 【读写模式】)
// 1.Context.MODE_PRIVATE 只能被本APP读写
// 2.Context.MODE_WORLD_READABLE 能被其他APP读,不能写
// 3.Context.MODE_WORLD_WRITEABLE 能被其他APP读写
// 23两种参数以不被官方推荐使用
SharedPreferences preferences=getSharedPreferences("user",MODE_PRIVATE);
// 获取读写数据的能力
SharedPreferences.Editor editor=preferences.edit();
//添加数据
editor.putInt("flag",1);
//清空所有数据
editor.clear();
//删除指定key的数据
editor.remove("flag");
//提交数据
editor.commit();
本地读取数据
SharedPreferences preferences=getSharedPreferences("user",MODE_PRIVATE);
//获取指定key-value,getXx(【key】,【如key不存在返回的默认值】);
preferences.getInt("flag",1);
//用来判断是否包含指定key的数据,返回boolean
preferences.contains("flag");
//获取全部的key-value,返回一个Map<String, ?>
preferences.getAll();