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 登录页面记住密码内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 详解Android通过修改配置文件设置wifi密码

    详解Android通过修改配置文件设置wifi密码 前言: 在一些非常规Android设备上,如眼镜/手表,输入wifi密码如同一场灾难.此时可以通过修改配置文件的方法设置wifi的ssid和密码. wifi密码配置文件 首先要保证设备已经root,wifi的配置文件在/data/misc/wifi/wpa_supplicant.conf,可以先将其pull出来,然后在下面加上network开头的那部分就ok了.然后再导入进去.下面是我的配置文件: ##### wpa_supplicant co

  • Android仿支付宝自定义密码输入框及安全键盘(密码键盘)

     0.前言 之前做过的项目里有运用到一个支付场景:用户办理业务时需要输入交易密码,并且可根据平台下发的支付方式进行选择.这与支付宝的密码输入方式十分相似,如果使用Android系统或者第三方软件的键盘,会有密码泄露的风险.因此,大多数的应用软件使用的是自定义的密码输入框及安全键盘. 由于密码输入方式需要实现一个从底部弹出的效果,因此总体上决定采用BottomSheetDialog来进行封装,同时为了提高安全性,还应该随机生成键盘上的数字,界面如下图所示:   首先新建一个PasswordInpu

  • Android Walker登录记住密码页面功能实现

    本文实例为大家分享了Android Walker登录记住密码页面的具体代码,供大家参考,具体内容如下 目标效果: 这一次修改的不多,添加了点击用户登录的跳转,登录页面的记住密码,和程序运行一次后,不进入导航页面的功能. 1.MainActivity.java页面修改了setOnItemClickListener的点击事件,进行跳转. MainActivity.java页面: package com.example.login; import java.util.ArrayList; import

  • Android利用CountDownTimer实现倒计时功能 Android实现停留5s跳转到登录页面

    利用CountDownTimer实现倒计时,停留5s跳转到登录页面功能,具体如下 举个栗子,引导页面最后一个界面要停留5s跳转到登录页面.代码如下: //假设 这是引导页面最后一个界面 public class MainActivity extends Activity { private TextView count_time; private MyCountDownTimer myCountDownTimer; @Override protected void onCreate(Bundle

  • Android 自定义密码输入框实现代码

    效果 自定义密码输入框,项目的一个界面需求,我把这个自定义的输入框提取出来作为这次内容的题目. 输入前: 输入后: 输入1个字符就红一个圈圈,很简单的效果. 思路 1.自定义EditText. 2.背景为一个外圆环加内实心圆. 3.edittext的长度变化时候重新绘制背景或者红色环位置. 关键代码 代码其实也很简单,顺手拿资源的请到文末. 1.画背景 /** * 绘制背景外圆 */ private void drawOutRing(Canvas canvas) { mPaint.setColo

  • 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" xmln

  • Android中使用SharedPreferences完成记住账号密码的功能

    效果图: 记住密码后,再次登录就会出现账号密码,否则没有. 分析: SharedPreferences可将数据存储到本地的配置文件中 SharedPreferences会记录CheckBox的状态,如果CheckBox被选,则将配置文件中记录的账号密码信息回馈给账号密码控件,否则清空. SharedPreferences使用方法: 1.创建名为config的配置文件,并且私有 private SharedPreferences config; config=getSharedPreference

  • Vue实现登录记住账号密码功能的思路与过程

    目录 实现思路 这里有三种方法来存储账号密码: 功能界面 记住账号密码功能的具体实现 密码加密 localStorage cookies 总结 实现思路 用户登录时若勾选"记住我"功能选项,则将登录名和密码(加密后)保存至本地缓存中,下次登录页面加载时自动获取保存好的账号和密码(需解密),回显到登录输入框中. 这里有三种方法来存储账号密码: 1. sessionStorage(不推荐) 1). 仅在当前会话下有效,关闭浏览器窗口后就被清除了 2). 存放数据大小一般为5MB 3). 不

  • Vue实现记住账号密码功能的操作过程

    目录 实现思路: 记住账号密码实现流程 npm安装base64依赖 实现思路: 用户登录时若勾选“记住我”功能选项,则将登录名和密码(加密后)存入本地缓存,下次登录页面加载时自动获取保存好的账号和密码(需解密),回显到登录输入框中. 说到存入本地缓存,大家想到的一定是cookies.localStorage.sessionStorage,不过后者我是不推荐使用的,咱们既然需求是记住密码那肯定是长时间或到下次取消时失效,但sessionStorage仅在当前会话下有效,关闭浏览器窗口后就被清除了,

  • Android实现记住账号密码功能

    本文实例为大家分享了Android实现记住账号密码的具体代码,供大家参考,具体内容如下 布局 一个复选框 <CheckBox android:id="@+id/checkbox" android:radius="5dp" android:text="记住我" android:layout_marginLeft="20dp" android:layout_width="wrap_content" and

  • JavaScript使用cookie实现记住账号密码功能

    很多登录功能上都有个"记住密码"的功能,其实无非就是对cookie的读取. 下面展示这个功能的代码,原作者已无法考究.... 测试方法:直接输入账号密码,提交后,刷新页面,再输入同样的账号,就可以显示 <!DOCTYPE HTML> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>

  • JAVA记住密码功能的实现代码

    准备:SSM框架,mysql数据库 用户表 user 实体类: public class User { /** * 主键id */ private Integer userId; /** * 账号 */ private String username; /** * 密码 */ private String password; public Integer getUserId() { return userId; } public void setUserId(Integer userId) {

  • vue实现禁止浏览器记住密码功能的示例代码

    查找资料 网上查到的一些方法: 使用 autocomplete="off"(现代浏览器许多都不支持) 使用 autocomplete="new-password" 在真正的账号密码框之前增加相同 name 的 input 框 使用 readonly 属性,在聚焦时移除该属性 初始化 input 框的 type 属性为 text,聚焦时修改为 password 使用 type="text",手动替换文本框内容为星号 "*" 或者

  • IOS NSUserDefault 记住用户名及密码功能的实例代码

    一般的登录界面都会有一个记住密码的选项,要实现这个功能可以使用NSUserDefault,这里只是讲解明文的处理方式,虽然这样是有一定的风险性的但是目前只是了解如何实现这个功能: 首先声明一个NSUserDefault对象: let userDefaults = NSUserDefaults.standardUserDefaults() //本地操作所需 然后根据是否记住密码按钮的状态来判断是否要为用户名和密码设置值,如果是记住密码,那么需要取出需要记住的密码,并且为这两个TextField赋值

  • Android实现显示和隐藏密码功能的示例代码

    在前端中我们知道用javascript就可以可以很容易实现,那么在Android中怎么实现这个功能呢? Java代码 package com.example.test2; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.method.HideReturnsTransformationMethod; import android.text.method.Pa

随机推荐