Android学习日记 Sharepreferences下的记住密码功能

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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值