移动技术开发:保存密码和自动登录

1 实验名称

       保存密码和自动登录

2 实验目的

       掌握利用SharedPreference实现记住密码和自动登录功能。

3 实验源代码

布局文件代码:

(1)activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TableRow>
        <TextView
            android:text="账号"
            android:layout_marginRight="10dp"
            android:textSize="18sp"
            />
        <EditText
            android:id="@+id/userNameET"
            android:hint="请输入账号"
            />
    </TableRow>

    <TableRow>
        <TextView
            android:text="密码"
            android:layout_marginRight="10dp"
            android:textSize="18sp"
            />
        <EditText
            android:id="@+id/passWordET"
            android:hint="请输入密码"
            />
    </TableRow>

    <TableRow>
        <CheckBox
            android:id="@+id/savePwdCB"
            android:text="记住密码"
            />
        <CheckBox
            android:id="@+id/autoLoginCB"
            android:text="自动登录"
            />

        <Button
            android:id="@+id/loginBtn"
            android:text="登录"
            />
    </TableRow>


</TableLayout>

(2)activity_login_success.xml

<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginSuccessActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录成功"
        />

</LinearLayout>

Java代码:

(1)MainActivity

package com.example.savelogininfotest;

import android.content.Context;
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.CompoundButton;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private EditText userNameET = null;
    private EditText passWordET = null;
    private CheckBox savePwdCB = null;
    private CheckBox autoLoginCB = null;
    private Button loginBtn = null;

    private String userName = null;
    private String userPwd= null;

    private SharedPreferences loginPreferences = null;
    private SharedPreferences.Editor loginEditor = null;

    private boolean isSavePwd = false;//用户是否选中记住密码的标志变量
    private boolean isAutoLogin = false;//用户是否选中自动登录的标志变量

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userNameET = findViewById(R.id.userNameET);
        passWordET = findViewById(R.id.passWordET);
        savePwdCB = findViewById(R.id.savePwdCB);
        autoLoginCB = findViewById(R.id.autoLoginCB);
        loginBtn = findViewById(R.id.loginBtn);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建存取用户名和密码以及是否记住密码和是否自动登录的标志变量的编辑器
                loginEditor = loginPreferences.edit();
                userName = userNameET.getText().toString().trim();//获取用户输入的用户名
                userPwd = passWordET.getText().toString().trim();//获取用户输入的密码
                //编辑器将用户名和密码以键值对的方式封装
                loginEditor.putString("userName",userName);
                loginEditor.putString("userPwd",userPwd);

                //编辑器将是否记住密码和是否自动登录的值以键值对的方式封装
                loginEditor.putBoolean("isSavePwd",savePwdCB.isChecked());
                loginEditor.putBoolean("isAutoLogin",autoLoginCB.isChecked());

                loginEditor.commit();//将前面的四个键值对提交存放到本地文件中

                welcomeLoad();//调用跳转到登录成功页面的方法
            }
        });

        loginPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);

        isSavePwd = loginPreferences.getBoolean("isSavePwd",false);
        isAutoLogin = loginPreferences.getBoolean("isAutoLogin",false);

        userName = loginPreferences.getString("userName",null);
        userPwd = loginPreferences.getString("userPwd",null);

        if (isAutoLogin){//判断用户上次是否选中了“自动登录”
            welcomeLoad();
        }else {
            loadLogin();
        }
    }

    //定义跳转到登录成功页面的方法
    private void welcomeLoad(){
        //跳转到“登录成功”
        Intent intent = new Intent(MainActivity.this, LoginSuccessActivity.class);
        startActivity(intent);
    }

    //定义登录方法
    private void loadLogin(){
        if (isSavePwd){//判断用户上次是否选中了“记住密码”
            userNameET.setText(userName);//将用户名填充到输入文本框
            passWordET.setText(userPwd);//将密码填充到输入文本框
            savePwdCB.setChecked(true);//将“记住密码”选择框勾选住
        }

        //为“自动登录”选择框添加事件监听
        autoLoginCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){//如果勾选了“自动登录”
                    savePwdCB.setChecked(true);//将“记住密码”选择框勾选住
                }
            }
        });
    }
}

(2)LoginSuccessActivity

package com.example.savelogininfotest;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class LoginSuccessActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_success);

    }
}

4 实验运行结果图

5 实验总结

       同之前一样,先写布局文件,有两个布局文件,第一个布局文件是自动登录和记住密码的界面,输入账号和密码,选择记住密码、自动登录;第二个布局文件是登录后的界面,显示登录成功。

       写完布局文件,开始写Java代码。定义两个方法,一个是跳转到登录成功页面的方法,另一个是登录方法;创建存取用户名和密码以及是否记住密码和是否自动登录的标志变量的编辑器,编辑器将用户名和密码以及是否记住密码和是否自动登录以键值对的方式封装,并将这四个键值对提交存放到本地文件中;关于自动登录还有一个,那就是判断上次登录是否选择自动登录,如果没有选择,就显示登录界面,如果上次登录时选择了自动登录,那么新一次登录时将自动跳转到登录成功的界面;关于记住密码,如果用户上次选择了记住密码,呐用户名和密码将填充到文本输入框。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

茜茜西西CeCe

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

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

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

打赏作者

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

抵扣说明:

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

余额充值