使用SharedPreferences实现记住密码功能

本文介绍了如何在Android应用程序中使用SharedPreferences类实现记住密码的功能。通过存储和读取键值对,SharedPreferences能帮助应用在用户下次打开时自动填充用户名和密码。详细步骤包括获取SharedPreferences对象、设置存储模式、存储数据、提交更改以及读取数据。

在我们平时用到的各种软件上,都有一个基本的登陆的功能。其中衍生出来的如记住密码这种功能,现在就来实现一下这个功能。

这里我们需要用到一个主要的类,SharedPreferences,这个类是用来存储比较简单的键值对(适合用于保存软件配置参数)。就如用户名或者密码这种。

使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data//shared_prefs目录下。

step#1 获取sharedprepreferences对象

SharedPreferences sharedPreferences = getSharedPreferences("userInfo", MODE_PRIVATE);

这里的两个参数,前面一个是存储的文件名,自己可以随便定义,第二个是存储的模式,也有几个选项,这里我们选择私有的模式。我们选两个比较典型的模式。

MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。

step#2 获取编辑器

editor = sharedPreferences.edit();

step#3 存储数据

 String name = mName.getText().toString();
 String password = mPassword.getText().toString();
 editor.putString("name", name);
 editor.putString("password", password);

step#4 提交(跟Fragment的提交有点相似)

editor.commit();

step#5 从sharedpreferences 中读取数据

String name = sharedPreferences.getString("name", "");
        String password = sharedPreferences.getString("password", "");

完整的代码
重点就是根据从sharedpreferences中得到数据是否为空来决定checkbox的状态是否为选中状态。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText mName, mPassword;
    private CheckBox mRemeberPass;
    private Button mLoign, mCancle;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mName = (EditText) findViewById(R.id.et_name);
        mPassword = (EditText) findViewById(R.id.et_password);
        mRemeberPass = (CheckBox) findViewById(R.id.cb_remember);
        mLoign = (Button) findViewById(R.id.btn_login);
        mCancle = (Button) findViewById(R.id.btn_cancle);
        mLoign.setOnClickListener(this);
        mCancle.setOnClickListener(this);
        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        String name = sharedPreferences.getString("name", "");
        String password = sharedPreferences.getString("password", "");
        if (password == null) {
            mRemeberPass.setChecked(false);
            mName.setTag(name);

        } else {
            mRemeberPass.setChecked(true);
            mName.setText(name);
            mPassword.setText(password);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String name = mName.getText().toString();
                String password = mPassword.getText().toString();
                if (name.equals("glc") && password.equals("123456")) {
                    if (mRemeberPass.isChecked()) {
                        editor.putString("name", name);
                        editor.putString("password", password);
                        editor.commit();
                    } else {
//                        editor.remove("name");
                        editor.remove("password");
                        editor.commit();
                    }
                    Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "登陆失败", Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }
}

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:orientation="vertical">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <CheckBox
        android:id="@+id/cb_remember"
        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:text="登陆" />

    <Button
        android:id="@+id/btn_cancle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消" />
</LinearLayout>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值