通过SharedPreferences/SP实现保存密码 学习笔记

本篇文章将介绍给大家通过SharedPreferences实现一种简单的记住密码的操作
先来看一下activity_main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/et_usename"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名:"
        />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:password="true"
        android:hint="请输入密码:"
        />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <CheckBox
            android:id="@+id/cb_ischeck"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"
             />

        <Button
            android:id="@+id/btn_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="login"
            android:text="登录"
             />

    </RelativeLayout>

</LinearLayout>

布局文件很简单,两个EditText用于输入用户名和密码,CheckBox用于选择是否记住密码,布局整体采用了垂直布局,ButtonCheckBox之间采用相对布局
下面是MainActivity中代码

package sptest.mystudy.kjpc.sptest;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText et_usename,et_password;
    private  CheckBox cb_ischeck;
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建实例,其中第一个参数即为生成的xml文件的文件名
        //第二个参数为用户对xml文件的操作权限
        sp = getSharedPreferences("config",0);
        et_usename =  findViewById(R.id.et_usename);
        et_password =  findViewById(R.id.et_password);
        cb_ischeck = findViewById(R.id.cb_ischeck);

        SharedPreferences.Editor edit = sp.edit();
        String name = sp.getString("name","");
        String pwd = sp.getString("pwd","");
        boolean ischecked = sp.getBoolean("checkState",false);
        et_usename.setText(name);
        et_password.setText(pwd);
        cb_ischeck.setChecked(ischecked);


    }

    //[2]写点击方法
    public void login(View v){
        String name = et_usename.getText().toString().trim();
        String pwd = et_password.getText().toString().trim();
         if(ischecked){
                SharedPreferences.Editor edit = sp.edit();
                //添加数据。第一个参数为键值,第二个参数为待添加的数据
                edit.putString("name",name);
                edit.putString("pwd",pwd);
                edit.putBoolean("checkState",ischecked);
                boolean isOK = edit.commit();
                if(isOK)
                    Toast.makeText(MainActivity.this,"存储成功",Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(MainActivity.this,"存储失败",Toast.LENGTH_LONG).show();
            }
            //如果未勾选,将文件中保存的用户名,密码和控件状态清空
            else{
                edit.putString("name", null);
                edit.putString("pwd",null);
                edit.putBoolean("checkState", false);
                edit.commit();
            }
        //判断用户名密码不为空
        if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)){
            Toast.makeText(MainActivity.this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
        }
      //判断用户登录是否合法,合法则执行
            else if (name.equals(new String("admin")) && pwd.equals(new String("123456"))) {
               Toast.makeText(MainActivity.this,"密码正确,登陆成功",Toast.LENGTH_SHORT).show();
            }
            //其他情况下认为用户名或密码错误
            else {
               Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
            }
    }

}

这里我们首先创建一个SP的实例,第一个参数为生成的xml文件的文件名(SP是以xml文件的形式保存数据的),第二个参数为文件操作权限,这里设置为私有。接着开启SP的Editor方法开启SP的编辑器,接下来的代码首先判断xml文件中有没有之前保存的用户名、密码以及控件状态,如果有,通过键值将已保存的数据取出并显示出来。在按钮点击事件中首先判断用户是否勾选记住密码,勾选则进行保存操作,并告诉用户保存是否成功,注意保存后一定要通过SP对象的commit()方法进行提交。如果未勾选记住密码,则清空之间保存的密码。
接着判断用户名和密码是否正确,这里我为了简便,直接在代码中设定好了用户名和密码,正确后弹出提示告知用户。
这里我使用得是Android Studio,用eclipse的小伙伴需要在findViewById设置一下向下转型
et_usename = (EditText)findViewById(R.id.et_usename);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值