Android——SharedPreference实践(强制下线添加密码记住功能)

在原本广播实践的代码上添加了一个记住密码的功能

修改登录界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".LoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Account"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/account"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Password"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:inputType="textPassword" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Remember Password" />

        <CheckBox
            android:id="@+id/remember_pass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login" />
</LinearLayout>

修改登录活动代码:

package com.example.broadcastbestpractice;

import android.content.Intent;
import android.content.SharedPreferences;
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 LoginActivity extends BaseActivity {
    private EditText account;
    private EditText password;
    private Button Login;
    private CheckBox checkBox;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private boolean isremember;
    private String s_account;
    private String s_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        account = findViewById(R.id.account);
        password = findViewById(R.id.password);
        checkBox = findViewById(R.id.remember_pass);
        Login = findViewById(R.id.login);
        sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
        isremember = sharedPreferences.getBoolean("isremember", false);
        if (isremember) {
            s_account = sharedPreferences.getString("account", "");
            s_password = sharedPreferences.getString("password", "");
            account.setText(s_account);
            password.setText(s_password);
            checkBox.setChecked(true);
        }
        Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s_account = account.getText().toString();
                s_password = password.getText().toString();
                if (s_account.equals("admin") && s_password.equals("123456")) {
                    editor=sharedPreferences.edit();
                    if (checkBox.isChecked()) {
                        editor.putString("account",s_account);
                        editor.putString("password",s_password);
                        editor.putBoolean("isremember",true);
                    }else {
                        editor.clear();
                    }
                    editor.apply();
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "密码或账号错误", Toast.LENGTH_SHORT).show();
                }
            }

        });
    }
}

首先在 onCreate()方法中获取到了 SharedPreferences对象,然后调用它 的 getBoolean()方法去获取 remember_password 这个键对应的值,一开始当然不存在对应的 值了,所以会使用默认值 false,这样就什么都不会发生。接着在登录成功之后,会调用 CheckBox的 isChecked()方法来检查复选框是否被选中,如果被选中了表示用户想要记住密 码,这时将 remember_password设置为 true,然后把 account和 password对应的值都存入到 SharedPreferences 文件当中并提交。如果没有被选中,就简单地调用一下 clear()方法,将 SharedPreferences文件中的数据全部清除掉。 当用户选中了记住密码复选框,并成功登录一次之后,remember_password键对应的值 就是 true了,这个时候如果再重新启动登录界面,就会从 SharedPreferences文件中将保存的 账号和密码都读取出来,并填充到文本输入框中,然后把记住密码复选框选中,这样就完成 记住密码的功能了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值