1、activity_main.xml文件样式

2、MainActivity.java主页内容

3、创建一个保存文件的类

1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"tools:context=".MainActivity"><ImageViewandroid:id="@+id/iv"android:layout_width="70dp"android:layout_height="70dp"android:layout_centerHorizontal="true"android:layout_marginTop="40dp"android:background="@drawable/dongman"/><LinearLayoutandroid:id="@+id/ll_number"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/iv"android:layout_centerVertical="true"android:layout_marginTop="15dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginBottom="5dp"android:background="#ffffff"><TextViewandroid:id="@+id/tv_number"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="账号"android:textColor="#000"android:textSize="20sp"/><EditTextandroid:id="@+id/et_number"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@null"android:padding="10dp"/></LinearLayout><LinearLayoutandroid:id="@+id/ll_password"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/ll_number"android:layout_centerVertical="true"android:layout_marginTop="15dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginBottom="5dp"android:background="#ffffff"><TextViewandroid:id="@+id/tv_password"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="密码"android:textColor="#000"android:textSize="20sp"/><EditTextandroid:id="@+id/et_password"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@null"android:padding="10dp"/></LinearLayout><Buttonandroid:id="@+id/btn_login"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/ll_password"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="30dp"android:text="登录"android:background="#3C8DC4"android:textSize="20sp"/></RelativeLayout>

2.MainActivity.java主页内容

package com.example.remembernp;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import java.util.Map;public class MainActivity extends AppCompatActivity implements View.OnClickListener{private EditText etNumber;private EditText etPassword;private Button btnLogin;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn = (Button)findViewById(R.id.btn_login);Map<String,String> userInfo = SaveFile.getUserInfo(this);initView();if(userInfo != null){etNumber.setText(userInfo.get("number"));etPassword.setText(userInfo.get("password"));}}private void  initView(){etNumber = (EditText)findViewById(R.id.et_number);etPassword = (EditText)findViewById(R.id.et_password);btnLogin = (Button)findViewById(R.id.btn_login);btnLogin.setOnClickListener(this);}@Overridepublic void onClick(View view) {//单击事件,获取账号密码String number = etNumber.getText().toString().trim();String password = etPassword.getText().toString().trim();//检查账号密码是否正确if(TextUtils.isEmpty(number)){Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();return;}if(TextUtils.isEmpty(password)){Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();return;}//否则登录成功Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();//保存信息boolean isSaveSuccess = SaveFile.saveUserInfo(this,number,password);if(isSaveSuccess){Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();}else{Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();}}
}

3.创建一个保存文件的类

package com.example.remembernp;import android.content.Context;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;public class SaveFile {//把账号密码保存在data.txt文件中public static boolean saveUserInfo(Context context, String number, String password){try{FileOutputStream fos = context.openFileOutput("data.txt",Context.MODE_PRIVATE);fos.write((number + ":" + password).getBytes());fos.close();return true;}catch (Exception e){e.printStackTrace();return false;}}//从data.txt中去获取刚刚保存的账号密码public static Map<String,String> getUserInfo(Context context) {String content = "";try {FileInputStream fis = context.openFileInput("data.txt");byte[] buffer = new byte[fis.available()];fis.read(buffer);//读取content = new String(buffer);Map<String ,String > userMap = new HashMap<String, String>();String[] infos = content.split(":");userMap.put("number",infos[0]);userMap.put("password",infos[1]);fis.close();return userMap;}catch (Exception e){e.printStackTrace();return null;}}
}

转载于:https://www.cnblogs.com/Mr-Deng/p/11313106.html

记住账号密码(本地存储)相关推荐

  1. android中注册的账号密码储存在,Android中使用SharedPreferences完成记住账号密码的功能...

    效果图: 记住密码后,再次登录就会出现账号密码,否则没有. 分析: SharedPreferences可将数据存储到本地的配置文件中 SharedPreferences会记录CheckBox的状态,如 ...

  2. 利用SharedPreferences完成记住账号密码的功能

    利用SharedPreferences完成记住账号密码的功能 效果图: 记住密码后,再次登录就会出现账号密码,否则没有. 分析: SharedPreferences可将数据存储到本地的配置文件中 Sh ...

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

    文章来源: 学习通http://www.bdgxy.com/ 目录 实现思路 这里有三种方法来存储账号密码: 功能界面 记住账号密码功能的具体实现 密码加密 localStorage cookies ...

  4. Vue实现记住账号密码功能

    实现思路: 用户登录时若勾选"记住我"功能选项,则将登录名和密码(加密后)存入本地缓存,下次登录页面加载时自动获取保存好的账号和密码(需解密),回显到登录输入框中. 说到存入本地缓 ...

  5. Jsp使用Cookie完成记住账号密码的功能

    网站中对于记住账号密码,方便下次登录的使用非常普遍,那么它是怎么实现的呢? 首先他的流程是,设计一个复选框,当选中复选框时,就会传值到处理页面,复选框的用途就是判断用户是否愿意记住账号密码. 我们通过 ...

  6. C# ASP.NET MVC:使用Cookie记住账号密码

    MVC记住账号密码 使用cookie操作 前端: 1 <div> 2 用户名:<input type="text" id="UserName" ...

  7. ssm实现记住账号密码(cookie)

    cookie实现记住账号密码 1.需要实现的功能? 2.实现思路 3.后端代码 1.需要实现的功能? 在我们登陆的时候,页面有一个记住密码的单选框,如果用户勾选了记住密码,那么下次登陆的时候,就无需输 ...

  8. android 记住多个账号,Android实现记住账号密码功能

    本文实例为大家分享了Android实现记住账号密码的具体代码,供大家参考,具体内容如下 布局 一个复选框 android:id="@+id/checkbox" android:ra ...

  9. uni-app 实现记住账号密码功能

    1.示例: 2.代码: 只看核心代码块 其他不用管 <template><view class="page"><view class="to ...

  10. QT 记住账号密码登录

    弄了个简单的记住账号密码登录功能,适合初学者学习,直接上代码,代码有注释. 喂饭到嘴边了,看你会不会吃啦 开发环境是VS2015 1.由于没有写注册功能,所以账号和密码我提前写在一个ini文件,这个文 ...

最新文章

  1. Vue 第一天: 计算属性和观察者
  2. aba问题mysql_解决CAS机制中ABA问题的AtomicStampedReference详解
  3. P of EAA 总结
  4. Spring在web开发中的应用
  5. filter过滤后重新添加_Spring Boot 2.X(十):自定义注册 Servlet、Filter、Listener
  6. IDEA解决Maven项目编译后classes文件中没有.xml或.properties问题
  7. Intellij IDEA 导入或运行流式处理框架storm以及java.lang.NoClassDefFoundError报错的解决方案
  8. css怎么居中字体,用CSS做将如何字体居中?
  9. 网游中的网络编程系列1:UDP vs. TCP
  10. 陪学网腾讯直播课堂开课啦~免费、免费、免费,重要的事情说三遍
  11. 7人表决器的实现之路
  12. 西部数据硬盘不同色彩的含义
  13. java汉字转拼音,pinyin4j简单介绍
  14. ❤️【独家】挑战全网最通俗易懂的神经网络的表达能力解释
  15. 共享单车之数据可视化
  16. 基于SpringBoot的健身房管理系统
  17. hdu 5238 Calculator(线段树+CRT)
  18. 【Javaweb】TCP原理(三次握手四次挥手)
  19. virtual 关键字
  20. Rocky Linux更换yum源为国内源

热门文章

  1. SAP CO系统配置-产品成本控制
  2. mybatis的parameterType属性那些情况下要写 哪些情况下不用写
  3. 【Vue实践】列表搜索框中模糊搜索功能的两种实现方式
  4. 关于nwjs自动更新的问题
  5. 2021年中式烹调师(中级)考试技巧及中式烹调师(中级)模拟考试
  6. 可视化案例研究——以智利总统选举为例
  7. 【Image Matting】 Deep Image Matting
  8. iOS XCode中的手机模拟器 利用键盘鼠标模拟各种手势 解决捏合手势中心不动的问题
  9. 【网络爬虫】爬取糗事百科段子
  10. SAP一句话入门:SD+PP+MM+FICO+HR(转)