Android制作登录页面并且记住账号密码功能的实现代码

 更新时间:2020年04月11日 10:02:02   作者:大亮  
这篇文章主要介绍了Android制作登录页面并且记住账号密码功能的实现代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、页面搭建

<?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">
  <EditText
    android:id="@+id/et_UserName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:ems="10"
    android:hint="请输入账号"
    android:inputType="textPersonName"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  <EditText
    android:id="@+id/et_Password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:ems="10"
    android:hint="请输入密码"
    android:inputType="textPassword"
    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="16dp"
    android:text="记住密码"
    app:layout_constraintStart_toStartOf="@+id/et_Password"
    app:layout_constraintTop_toBottomOf="@+id/et_Password" />
  <Button
    android:id="@+id/button"
    android:onClick="Login"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="24dp"
    android:layout_marginLeft="24dp"
    android:layout_marginEnd="24dp"
    android:layout_marginRight="24dp"
    android:text="安全登录"
    app:layout_constraintBottom_toBottomOf="@+id/checkBox"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/checkBox"
    app:layout_constraintTop_toTopOf="@+id/checkBox" />
</android.support.constraint.ConstraintLayout>

二、代码实现

package com.hiscene.test;
import android.support.v7.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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
  EditText et_userName;
  EditText et_password;
  CheckBox checkBox;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_layout);
    et_userName = findViewById(R.id.et_UserName);
    et_password = findViewById(R.id.et_Password);
    checkBox = findViewById(R.id.checkBox);
    LoadInfo();
  }
  private void LoadInfo()
  {
    File file=new File("data/data/com.hiscene.test/usre.txt");
    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();
    }
  }
  public void Login(View view) {
    String userName=et_userName.getText().toString().trim();
    String password= et_password.getText().toString().trim();
    if (TextUtils.isEmpty(userName)|| TextUtils.isEmpty(password))
    {
      Toast.makeText(MainActivity.this, "用户名或密码不能为空!", Toast.LENGTH_SHORT).show();
      return;
    }
    if (checkBox.isChecked())
    {
      File file=new File("data/data/com.hiscene.test/usre.txt");
      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();
      }
    }
  }
}

总结

到此这篇关于Android制作登录页面并且记住账号密码功能的实现代码的文章就介绍到这了,更多相关android 登录页面记住密码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android的搜索框架实例详解

    Android的搜索框架实例详解

    这篇文章主要介绍了Android的搜索框架实例详解的相关资料,非常不错,具有参考借鉴价值,感兴趣的朋友一起看看吧
    2016-10-10
  • Android Studio安装配置、环境搭建详细步骤及基本使用的详细教程

    Android Studio安装配置、环境搭建详细步骤及基本使用的详细教程

    这篇文章主要介绍了Android Studio安装配置、环境搭建详细步骤及基本使用的详细教程,需要的朋友可以参考下
    2020-03-03
  • Android 使用Toolbar实现应用栏实例详解

    Android 使用Toolbar实现应用栏实例详解

    这篇文章主要为大家介绍了Android 使用Toolbar实现应用栏实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Android控件View的文字周围添加图标

    Android控件View的文字周围添加图标

    这篇文章主要为大家详细介绍了Android控件View的文字周围添加图标,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-03-03
  • Android开发岛屿数量算法示例解析

    Android开发岛屿数量算法示例解析

    这篇文章主要为大家介绍了Android开发岛屿数量算法示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • flutter PageView实现左右滑动切换视图

    flutter PageView实现左右滑动切换视图

    这篇文章主要为大家详细介绍了flutter PageView实现左右滑动切换视图,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • 解决Android使用Handler造成内存泄露问题

    解决Android使用Handler造成内存泄露问题

    内存泄露的危害就是会使虚拟机占用内存过高,导致OOM(内存溢出),程序出错。接下来通过本文给大家分享Android使用Handler造成内存泄露问题及解决方法,一起看看吧
    2017-08-08
  • Flutter实现网络请求的方法示例

    Flutter实现网络请求的方法示例

    这篇文章主要介绍了Flutter实现网络请求的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • Android中的设计模式

    Android中的设计模式

    常用的设计模式有以下八种:单例、工厂、观察者、代理、命令、适配器、合成、访问者。下面通过本文给大家介绍下android中的设计模式,感兴趣的朋友一起看看吧
    2016-09-09
  • Android开发实现简单的观察者与被观察者示例

    Android开发实现简单的观察者与被观察者示例

    这篇文章主要介绍了Android开发实现简单的观察者与被观察者,简单描述了观察者模式的概念、原理并结合实例形式分析了Android实现观察者模式的简单操作技巧,需要的朋友可以参考下
    2017-11-11

最新评论