记住密码(Android登录界面)

2024-06-03 00:08

本文主要是介绍记住密码(Android登录界面),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在编写安卓登录界面时,我们如何记住密码,方便下一次登录呢?

首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"记住密码"

然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.csdn.www.MainActivity" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名" /><EditTextandroid:id="@+id/username"android:layout_width="fill_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/textView2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="密码" /><EditTextandroid:id="@+id/userpass"android:layout_width="fill_parent"android:layout_height="wrap_content"android:inputType="textPassword"android:password="true" /><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content" ><Buttonandroid:id="@+id/login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:layout_marginRight="73dp"android:text="登陆" /><CheckBoxandroid:id="@+id/checkBox1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:checked="true"android:text="记住密码" /></RelativeLayout></LinearLayout>

视图效果:

在src下的java文件里创建一个SaveFile.java类,旨在记住密码之后如何保存用户名、密码和获取保存数据

package com.csdn.www;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;import android.content.Context;public class SaveFile {public static boolean save(Context context,String name,String pass){try {//File f=new File("/data/data/com/csdn/www/info.txt");File f=new File(context.getFilesDir(),"info.txt");//context.getFilesDir();//返回一个目录  /data/data/包名/filesFileOutputStream fos=new FileOutputStream(f);fos.write((name+"=="+pass).getBytes());fos.close();return true;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}}/*** 获取保存的数据* @param context* @return*/public static Map<String,String> getSaveFiles(Context context){File f=new File(context.getFilesDir(),"info.txt");try {FileInputStream fis=new FileInputStream(f);BufferedReader br=new BufferedReader(new InputStreamReader(fis));String str=br.readLine();String[] infos=str.split("==");Map<String,String> map=new HashMap<String,String>();map.put("username", infos[0]);map.put("userpass", infos[1]);return map;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}}
}

最后在src下的java文件里MainActivity.java

package com.csdn.www;import java.util.Map;import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity extends ActionBarActivity {private static final String TAG = "MainActivity";private EditText username, userpass;private CheckBox checkBox1;private Button login;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.fragment_main);username = (EditText) this.findViewById(R.id.username);userpass = (EditText) this.findViewById(R.id.userpass);checkBox1 = (CheckBox) this.findViewById(R.id.checkBox1);Map<String, String> map=SaveFile.getSaveFiles(this);if(map!=null){username.setText(map.get("username"));userpass.setText(map.get("userpass"));}login = (Button) findViewById(R.id.login);login.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String un = username.getText().toString().trim();String up = userpass.getText().toString().trim();if (TextUtils.isEmpty(un) || TextUtils.isEmpty(up)) {Toast.makeText(MainActivity.this, "用户名或密码不能为空",Toast.LENGTH_SHORT).show();} else {// 登陆//是否保存密码if (checkBox1.isChecked()) {// 保存用户名和密码Log.i(TAG, "需要保存用户名密码");boolean flag=SaveFile.save(MainActivity.this,un, up);if(flag){Toast.makeText(MainActivity.this, "信息保存成功", 0).show();}}// 登陆发送消息到服务器,服务器验证是否正确if ("zhangsan".equals(un) && "123".equals(up)) {Toast.makeText(MainActivity.this, "登陆成功",Toast.LENGTH_SHORT).show();} else {Toast.makeText(MainActivity.this, "用户名或密码错误",Toast.LENGTH_SHORT).show();}}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}


会显示“信息保存成功”,“用户名或密码错误”

在Eclipse中的”File Explorer“里,目录”data/data/com/csdn/www/info.txt“,用户名和密码会保存在info.txt里


这篇关于记住密码(Android登录界面)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1025478

相关文章

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

JWT + 拦截器实现无状态登录系统

《JWT+拦截器实现无状态登录系统》JWT(JSONWebToken)提供了一种无状态的解决方案:用户登录后,服务器返回一个Token,后续请求携带该Token即可完成身份验证,无需服务器存储会话... 目录✅ 引言 一、JWT 是什么? 二、技术选型 三、项目结构 四、核心代码实现4.1 添加依赖(pom

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

Spring Security重写AuthenticationManager实现账号密码登录或者手机号码登录

《SpringSecurity重写AuthenticationManager实现账号密码登录或者手机号码登录》本文主要介绍了SpringSecurity重写AuthenticationManage... 目录一、创建自定义认证提供者CustomAuthenticationProvider二、创建认证业务Us

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Springboot项目登录校验功能实现

《Springboot项目登录校验功能实现》本文介绍了Web登录校验的重要性,对比了Cookie、Session和JWT三种会话技术,分析其优缺点,并讲解了过滤器与拦截器的统一拦截方案,推荐使用JWT... 目录引言一、登录校验的基本概念二、HTTP协议的无状态性三、会话跟android踪技术1. Cook

使用Redis快速实现共享Session登录的详细步骤

《使用Redis快速实现共享Session登录的详细步骤》在Web开发中,Session通常用于存储用户的会话信息,允许用户在多个页面之间保持登录状态,Redis是一个开源的高性能键值数据库,广泛用于... 目录前言实现原理:步骤:使用Redis实现共享Session登录1. 引入Redis依赖2. 配置R

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、