Android realizes simple password saving function

1. Introduction

        A small case, save the user name and password entered by the user into a txt format file, and read it from the local cache the next time it is opened. The character stream is used to save. The account password is stored in plain text, which is not done in real commercial applications. This case is just to introduce how to operate the character stream.

 

2. Case realization (code is as follows)

1. Building UI

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <EditText
        android:id="@+id/et_username"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="请输入用户名"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword"
        android:hint="请输入密码"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et_username" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="记住密码"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et_password" />

    <Button
        android:id="@+id/bt_login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:text="安全登录"
        android:onClick="OnClickButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/checkBox"
        app:layout_constraintTop_toBottomOf="@+id/et_password" />
</androidx.constraintlayout.widget.ConstraintLayout>

2. Business logic

package com.hiscene.test03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {

   private EditText et_username;
   private EditText et_password;
   private CheckBox checkBox;
   private final String usertxtname="user.txt";
   private File fileDir;

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

        fileDir=MainActivity.this.getFilesDir();
        et_username=findViewById(R.id.et_username);
        et_password=findViewById(R.id.et_password);
        checkBox=findViewById(R.id.checkBox);

        LoadCacheUserInfo();
    }

    //按钮点击事件
    public void OnClickButton(View view)
    {
        if (view.getId() == R.id.bt_login) {
            String userName = et_username.getText().toString().trim();
            String password = et_password.getText().toString().trim();
            Login(userName, password);
        }
    }

    //登录
    private void Login(String userName,String password) {
        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(password)) {
            Toast.makeText(MainActivity.this, "用户名和密码不能为空!", Toast.LENGTH_LONG).show();
        } else {

            //链接服务器进行登录验证Todo....

            if (checkBox.isChecked()) SaveUserInfo(userName, password);
        }
    }

    //保存用户信息
    private void SaveUserInfo(String userName,String password) {

        File file = new File(fileDir,usertxtname);
        try {
            OutputStream out = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");
            BufferedWriter writer = new BufferedWriter(osw);
            writer.write(userName + "#" + password);
            writer.flush();
            writer.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取缓存中的用户数据
    private void LoadCacheUserInfo()
    {
        File file = new File(fileDir,usertxtname);
        if (!file.exists())return;

        try {
            FileReader reader =new FileReader(file);
            BufferedReader br=new BufferedReader(reader);
            String text=br.readLine();
            String[] arr=text.split("#");
            et_username.setText(arr[0]);
            et_password.setText(arr[1]);
            checkBox.setChecked(true);
            br.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

Source code download: link: https://pan.baidu.com/s/1ncgNCem4OnL8ixWQ_4VWSA Password: c05e

Guess you like

Origin blog.csdn.net/a451319296/article/details/109501203