Android学习:使用SharedPreferences实现用户登录记住密码功能

一、对界面进行布局,布局如下图所示

(1)布局界面

(2)string.xml的内容如下所示:

<resources>
    <string name="app_name">记住密码案例</string>
    <string name="user_name">用户名:</string>
    <string name="user_pwd">密码:</string>
    <string name="check_name">记住密码</string>
    <string name="login">登 录</string>
    <string name="success">登录成功</string>

</resources>

(2)activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <EditText
        android:id="@+id/pwdEdit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:hint="请输入密码"
        android:inputType="textPassword"
        app:layout_constraintBaseline_toBaselineOf="@+id/userpwd"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/userEdit" />

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="9dp"
        android:text="@string/user_name"
        android:textSize="20dp"
        app:layout_constraintBottom_toTopOf="@+id/userpwd"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/userpwd"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="412dp"
        android:text="@string/user_pwd"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/username"
        app:layout_constraintTop_toBottomOf="@+id/username" />

    <EditText
        android:id="@+id/userEdit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:hint="请输入用户名"
        app:layout_constraintBottom_toTopOf="@+id/userpwd"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/loginBtn"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/rememberBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="18dp"
        android:text="@string/check_name"
        app:layout_constraintBottom_toTopOf="@+id/loginBtn"
        app:layout_constraintEnd_toEndOf="@+id/userpwd"
        app:layout_constraintStart_toEndOf="@+id/userpwd"
        app:layout_constraintTop_toBottomOf="@+id/userpwd" />

    <Button
        android:id="@+id/loginBtn"
        android:layout_width="123dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="34dp"
        android:layout_marginLeft="34dp"
        android:layout_marginBottom="314dp"
        android:text="@string/login"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/rememberBtn"
        app:layout_constraintTop_toBottomOf="@+id/rememberBtn" />

</android.support.constraint.ConstraintLayout>

二、实现记住密码的功能

(1)代码如下:

package cn.edu.sxy.remeberpwddemo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
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.Toast;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences.Editor editor;
    private SharedPreferences spf;
    private EditText userEdit,pwdEdit;
    private CheckBox rememberPwd;
    private Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spf = PreferenceManager.getDefaultSharedPreferences(this);
        userEdit = findViewById(R.id.userEdit);
        pwdEdit = findViewById(R.id.pwdEdit);
        rememberPwd = findViewById(R.id.rememberBtn);
        login = findViewById(R.id.loginBtn);
        boolean isRem = spf.getBoolean("remeberPwd", false);
        if (isRem) {
            //若remeberPwd为true则通过SharedPreferences对象获取文件中的数据
            String username = spf.getString("username", "");
            String password = spf.getString("password", "");
            //将数据显示在文本框中
            userEdit.setText(username);
            pwdEdit.setText(password);
            rememberPwd.setChecked(true);
        }
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取从文本框中输入的文本信息
                String username = userEdit.getText().toString();
                String password = pwdEdit.getText().toString();
                //设置默认的账号是admin,密码是123456,则登录成功
                if ("admin".equals(username) && "123456".equals(password)) {
                    editor = spf.edit();
                    //判断复选框是否被选中,将输入的数据存入到SharedPreferences文件中
                    if (rememberPwd.isChecked()) {
                        editor.putBoolean("remeberPwd", true);
                        editor.putString("username", username);
                        editor.putString("password", password);
                    } else {
                        //若复选框没有被选中,调用clear()方法,清除SharedPreferences文件中的数据
                        editor.clear();
                    }
                    editor.apply();
                    //用户名和密码正确,跳转页面
                    Intent intent = new Intent(MainActivity.this, Success.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(MainActivity.this, "用户名和密码已记住", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

(2)另外还需要创建一个新的activity活动,用于登录成功之后跳转的页面 。 

(3)测试结果图:

  • 输入正确的用户名和密码,选中记住密码复选框,点击登录

  • 跳转到登录成功页面
  • 关闭之后,重新运行程序会发现用户名和密码已经被记住默认显示在页面

    注:只是简单的演示登录时的记住密码功能

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shexianyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值