FileSaveQQ.java

package njitt.software.saveqq;import android.content.Context;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;public class FileSaveQQ {//保存QQ账号和登录密码到data.txt文件中public static boolean saveUserInfo(Context context, String account, Stringpassword) {FileOutputStream fos = null;try {//获取文件的输出流对象fosfos = context.openFileOutput("data.txt",Context.MODE_PRIVATE);//将数据转换为字节码的形式写入data.txt文件中fos.write((account + ":" + password).getBytes());return true;} catch (Exception e) {e.printStackTrace();return false;}finally {try {if(fos != null){fos.close();}} catch (IOException e) {e.printStackTrace();}}}//从data.txt文件中获取存储的QQ账号和密码public static Map<String, String> getUserInfo(Context context) {String content = "";FileInputStream fis = null;try {//获取文件的输入流对象fisfis = context.openFileInput("data.txt");//将输入流对象中的数据转换为字节码的形式byte[] buffer = new byte[fis.available()];fis.read(buffer);//通过read()方法读取字节码中的数据content = new String(buffer); //将获取的字节码转换为字符串Map<String, String> userMap = new HashMap<String, String>();String[] infos = content.split(":");//将字符串以“:”分隔后形成一个数组的形式userMap.put("account", infos[0]);   //将数组中的第一个数据放入userMap集合中userMap.put("password", infos[1]); //将数组中的第二个数据放入userMap集合中return userMap;} catch (Exception e) {e.printStackTrace();return null;}finally {try {if(fis != null){fis.close();}} catch (IOException e) {e.printStackTrace();}}}
}

MainActivity.java

package njitt.software.saveqq;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 et_account;   //账号输入框private EditText et_password; //密码输入框private Button btn_login;      //登录按钮@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();//通过工具类FileSaveQQ中的getUserInfo()方法获取QQ账号和密码信息// Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);Map<String, String> userInfo = SPSaveQQ.getUserInfo(this);if (userInfo != null) {et_account.setText(userInfo.get("account"));   //将获取的账号显示到界面上et_password.setText(userInfo.get("password")); //将获取的密码显示到界面上}}private void initView() {et_account = (EditText) findViewById(R.id.et_account);et_password = (EditText) findViewById(R.id.et_password);btn_login = (Button) findViewById(R.id.btn_login);//设置按钮的点击监听事件btn_login.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_login://当点击登录按钮时,获取界面上输入的QQ账号和密码String account = et_account.getText().toString().trim();String password = et_password.getText().toString();//检验输入的账号和密码是否为空if (TextUtils.isEmpty(account)) {Toast.makeText(this, "请输入QQ账号", 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 = FileSaveQQ.saveUserInfo(this, account,password);/*  boolean isSaveSuccess = SPSaveQQ.saveUserInfo(this, account, password);*/if (isSaveSuccess) {Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();}break;}}
}

SPSaveQQ.java

package njitt.software.saveqq;import android.content.Context;
import android.content.SharedPreferences;import java.util.HashMap;
import java.util.Map;public class SPSaveQQ{// 保存QQ账号和登录密码到data.xml文件中public static boolean saveUserInfo(Context context, String account,String password) {SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE);SharedPreferences.Editor edit = sp.edit();edit.putString("userName", account);edit.putString("pwd", password);edit.commit();return true;}//从data.xml文件中获取存储的QQ账号和密码public static Map<String, String> getUserInfo(Context context) {SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE);String account = sp.getString("userName", null);String password = sp.getString("pwd", null);Map<String, String> userMap = new HashMap<String, String>();userMap.put("account", account);userMap.put("password", password);return userMap;}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#E6E6E6"android:orientation="vertical"android:padding="10dp"><ImageViewandroid:layout_width="70dp"android:layout_height="70dp"android:layout_centerHorizontal="true"android:layout_gravity="center_horizontal"android:layout_marginTop="30dp"android:src="@drawable/head" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="15dp"android:background="@android:color/white"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="账号:"android:textColor="#000"android:textSize="20sp" /><EditTextandroid:id="@+id/et_account"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@null"android:padding="10dp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:background="@android:color/white"android:orientation="horizontal"><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:inputType="textPassword"android:padding="10dp" /></LinearLayout><Buttonandroid:id="@+id/btn_login"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="25dp"android:background="#3C8DC4"android:text="登录"android:textColor="@android:color/white"android:textSize="20sp" />
</LinearLayout>

实现效果如下:

《Android移动应用基础教程》之保存QQ账号和密码相关推荐

  1. 【Android实战】保存QQ账号与密码

    大家好,我是汤姆凯特. 写在前面:今天用保存QQ账号和密码的实战演练,带大家掌握Android存储中最基本的文件存储方式 文件存储是Android中最基本的一种数据存储方式,它与Java中的文件存储类 ...

  2. 安卓---第5章 数据存储---保存QQ账号与密码

    文章目录 案例1: 使用文件存储 保存QQ账号与密码 功能描述 saveqq_1.xml FileSaveQQ.java saveqq_1 案例2 使用SP保存QQ账号与密码 功能描述 SPSaveQ ...

  3. android 帐号密码xml,《Android移动应用基础教程》之保存QQ账号和密码

    FileSaveQQ.java package njitt.software.saveqq; import android.content.Context; import java.io.FileIn ...

  4. 实战演练--保存QQ账号与密码

    1.布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...

  5. Android的SharePreferences存储的案例(qq账号存储)

    使用SharedPreferences类存储数据时,首先需要调用getSharedPreferences(String name,int mode)方法获取实例对象.由于该对象本身只能获取数据,不能对 ...

  6. android移动应用基础教程--qq账号与密码

    android移动应用基础教程--qq账号与密码 android移动应用基础教程p115案例 实战演练-保存QQ账号密码. activity_main.xml <?xml version=&qu ...

  7. 《Android移动应用基础教程》(Android Studio)(第二版)黑马程序员 课后习题答案

    <Android移动应用基础教程>(Android Studio)(第二版)黑马程序员 课后习题答案 目录 第1章 Android基础入门 第2章 Android常见界面布局 第3章 An ...

  8. 《Android 移动应用基础教程(Android Studio)(第2版)》【课本客观题】+【学习通2023春】【参考答案】

    文章目录 超星学习通智能终端软件开发(基于Android Studio环境)章节作业(39) 一 二 三 四 五 六 课本一 课本二 课本三 课本四 课本五 课本六(无) 课本七 课本八 课本九 课本 ...

  9. 创建android程序时 默认使用布局是,《Android移动应用基础教程》中国铁道出版社课后习题(附答案)...

    <Android移动应用基础教程>中国铁道出版社课后习题(附答案) 第2章Android UI开发 一.填空题 1.Android中的布局分为6种,分别是RelativeLayout.Li ...

最新文章

  1. ALV标准范例Demo汇总
  2. ueditor1_4_3_3编辑器的应用
  3. C++的精髓——虚函数
  4. jsp压缩html,使用HtmlCompressor压缩JSP编译的Html代码
  5. 集群,分布式,微服务的区别
  6. 信奥中的数学:计算几何
  7. JDK 中的证书生成和管理工具 keytool
  8. 目标检测java系统_5分钟!用Java实现目标检测
  9. MYSQL异常处理日志:主从库同步延迟时间过长的分析
  10. HTC 手柄扣动板机出现射线以及碰撞点用小球表示
  11. Office - Excel 2013
  12. activiti7的网关
  13. ADI高速信号采集芯片与JESD204B接口简介
  14. 故事是如何改变人生的
  15. linux sox录音时间控制,Linux 对音频万能处理的命令——SOX
  16. oraoledb.oracle.11,Oracle11g链接提示未“在本地计算机注册“OraOLEDB.Oracle”解决方法...
  17. scp 传目录_scp拷贝文件及文件夹
  18. 二维数组主对角线与次对角线输出(C语言)
  19. 天平游码读数例题_“天平”试题归类例析
  20. Linux从一般用户切换到root用户

热门文章

  1. 如何更新VLC媒体播放器
  2. scp 保留文件属组_scp 对拷文件夹 和 文件夹下的所有文件 对拷文件并重命名
  3. 一起Talk Android吧(第三百八十二回:UI框架)
  4. 电脑为什么丢失msvcp140.dll?msvcp140.dll丢失修复详细教程步骤
  5. Web3.0、区块链和韭菜
  6. SpaceClaim脚本功能(Beta功能)
  7. 关于MATLAB2019a的三相全控桥整流电路的画法学习
  8. 中国煤化工行业十四五战略目标与发展建议分析报告2022版
  9. suse linux关机命令行,suse linux 关机命令
  10. 天意u盘启动盘安装linux,(BIOS+UEFI双启WINPE)天意u盘维护系统技术员版V2.1