Android实现登陆界面的记住密码功能

本文实例为大家分享了Android实现登陆界面记住密码功能的具体代码,供大家参考,具体内容如下

所需工具

一、复选框控件:CheckBox,
二、SharedPreferences用于存储数据,该工具的读取和写入较为简单,放在代码里的注释中解释

实现逻辑:

如果没弄懂逻辑,代码看起来还是有点小难度的

一、判断SharedPreferences中已存入的CheckBox的Boolean信息(没有读取到则默认条件为“否”),如果条件为“是”(同时满足能读取到和读取的信息为“是”两个条件),通过SharedPreferences将存储的数据(account和password)读取出来并写入对应的文本框。

二、点击登录按键时,判断CheckBox是否勾选,如果条件为“是”,则将accout和password框里的数据(String)以及CheckBox的数据(Boolean)写入SharedPreferences,若没有勾选,则清除SharedPreferences中的数据。

实现代码

一、ui界面

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity">
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="60dp">
    <TextView
        android:layout_width="90dp"
        android:text="Account"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_gravity="center"
        />
    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:id="@+id/account"/>

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp">
    <TextView
        android:layout_width="90dp"
        android:text="Password"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_gravity="center"/>
    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:id="@+id/password"/>
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/remember_pass"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="Rember password"/>
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="LogIn"
        android:id="@+id/login"/>
</LinearLayout>

二、实现功能部分

package com.example.broadcastbestpractice;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
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 accountEdit;
    private EditText passwordEdit;
    private Button login;
    private SharedPreferences pref;//通过pref读取SharedPreferences的数据
    private SharedPreferences.Editor editor;//editor将数据写入SharedPreferences
    private CheckBox rememberPass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        pref= PreferenceManager.getDefaultSharedPreferences(this);
        accountEdit = (EditText) findViewById(R.id.account);
        passwordEdit = (EditText) findViewById(R.id.password);
        rememberPass=(CheckBox)findViewById(R.id.remember_pass);
        login = (Button) findViewById(R.id.login);
        boolean isRemenber=pref.getBoolean("remember_password",false);//读取上次登陆时存入"remember_password"的信息,没有读取到则默认为false
        if(isRemenber)//如果读取为true,则将account和password,checkbox的信息写入文本框
        {
            String account=pref.getString("account","");
            String password=pref.getString("password","");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            rememberPass.setChecked(true);
        }
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String accout = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                if (accout.equals("1") && password.equals("1")) {
                    editor=pref.edit();
                    if(rememberPass.isChecked()){//如果勾选了checkbox框,则将account,password,checkbox信息写入
                        editor.putBoolean("remember_password",true);
                        editor.putString("account",accout);
                        editor.putString("password",password);
                    }else {
                        editor.clear();//若没有,清除SharedPreferences存储的信息
                    }
                    editor.apply();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else
                    Toast.makeText(LoginActivity.this, "account or password is wrong", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

功能完善

我在app内部添加了一个强制下线功能,这样,进入app后点击下线按钮就能直接退出到登陆界面,会使效果更加直观。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android实现登录界面记住密码的存储

    Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入.所以比较适合我们今天做的这个项目.我们来看一下运行图: 一.布局界面 1.login_top.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.a

  • Android实现带有记住密码功能的登陆界面

    本文实例为大家分享了Android带有记住密码功能的登陆界面实现代码,供大家参考,具体内容如下 1.设计思路 主要采用SharedPreferences来保存用户数据,本Demo没有经过加密,所有一旦Android系统被ROOT的话,其他用户就可以查看用户的私有目录,密码文件就很不安全.所以真正应用在软件上面的,一定要经过加密才保存,可以选择MD5加密. SharedPreferences介绍可以参看这篇博文:http://www.jb51.net/article/84859.htm TextW

  • Android开发笔记SQLite优化记住密码功能

    本文实例为大家分享了Android SQLite优化记住密码功能的具体代码,供大家参考,具体内容如下 package com.example.alimjan.hello_world; /** * Created by alimjan on 7/4/2017. */ import com.example.alimjan.hello_world.bean.UserInfo; import com.example.alimjan.hello_world.dataBase.UserDBHelper; i

  • Android通过"记住密码"功能学习数据存储类SharedPreferences详解及实例

    SharedPreferences是Android中存储简单数据的一个工具类.可以想象它是一个小小的Cookie,它通过用键值对的方式把简单数据类型(boolean.int.float.long和String)存储在应用程序的私有目录下(data/data/包名/shared_prefs/)自己定义的xml文件中. 一.简介 它提供一种轻量级的数据存储方式,通过eidt()方法来修改里面的内容,通过Commit()方法来提交修改后的内容. 二.重要方法 public abstract boole

  • Android sharedPreferences实现记住密码功能

    实现记住密码功能,供大家参考,具体内容如下 编写界面交互代码: package com.example.bz0209.login; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bund

  • Android实现记住密码功能

    本文实例为大家分享了Android实现记住密码功能的具体代码,供大家参考,具体内容如下 LoginActivity.java package com.wangdeqiang.www.chatwithrobot.BroadcastBestPractice; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preferen

  • Android实现用户登录记住密码功能

    一.打开之前完成的Case_login进行修改再编辑 二.将注册按钮删除并在登录按钮前拖出一个checkBox,编辑代码如下: 在layout_top.xml文件中进行编辑 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_

  • Android 使用SharedPreferrences储存密码登录界面记住密码功能

    Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入.所以比较适合我们今天做的这个项目.我们来看一下运行图: 一.布局界面 1.login_top.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.a

  • Android SharedPreferences实现记住密码和自动登录界面

    SharedPreferences介绍: SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置参数,它是采用xml文件存放数据的,文件存放在"/data/data<package name>/shared_prefs"目录下. SharedPreferences的用法: 由于SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力.但它是通过其Editor接口中的一些方法来操作Shared

  • Android SharedPreferences实现记住密码和自动登录

    本文实例为大家分享了Android SharedPreferences实现记住密码和自动登录,供大家参考,具体内容如下 效果图: 第一次进入进来 勾选记住密码和自动登录成功后,第二次进来 说明:中间存在的图片或者多余的其他部分可删掉.留下最主要的填写部分和登陆按钮即可.功能还是可以实现的. XML文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="h

随机推荐