Android之模仿QQ保存账号密码

news/2024/10/21 20:52:47/

先写个布局文件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="Login"android:background="#3C8DC4"android:textSize="20sp"/></RelativeLayout>

然后创建一个FileSaveQQ的类,用来实现QQ的账号密码保存

package com.example.mrlee.login;import android.content.Context;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;public class FileSaveQQ {//把账号密码保存在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;}}
}

saveUserInfo()方法呢是将数据保存在data.txt文件中,而getUserInfo()方法则是将数据从data.txt中读取出来
接下来就编写主方法MainActivity.java

package com.example.mrlee.login;import android.content.Intent;import android.support.v7.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 button1 = (Button) findViewById(R.id.btn_login);initView();Map<String,String> userInfo = FileSaveQQ.getUserInfo(this);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 v){//单击事件,获取账号密码String number = etNumber.getText().toString().trim();String password = etPassword.getText().toString();//检查账号密码是否正确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 = FileSaveQQ.saveUserInfo(this,number,password);if (isSaveSuccess){Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();}else {Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();}}
}

initView()是初始化控件,单击时调用FileSaveQQ类中的saveUserInfo()方法进行数据的保存
运行成功后,可以通过单击DDMS视图右上方的导出图标按钮将文件导出,就可以看淡刚刚保存的数据啦


http://www.ppmy.cn/news/152109.html

相关文章

QQ密码天使v1.0开发小记

首行申明一下&#xff0c;写这个程序完全是一时的兴趣&#xff0c;是自己一个阶段学习的总结而已!程序没有发表&#xff0c;毕竟这个程序多多少少有点害人!L只所以叫“密码天使”&#xff0c;嘻嘻&#xff0c;形象撒&#xff01;让你意想不到会收到这么多ID、密码&#xff0c;自…

浅析QQ密码保护原理

好久没写博客了&#xff0c;只因为最近一段时间着手学习网络入侵与渗透这块&#xff0c;正好借了一个大学生创业孵化基地的项目&#xff0c;也申请了一个关于“蜜罐”设计的科研立项&#xff0c;准备一起给做了&#xff0c;由于刚刚踏入未知领地&#xff0c;也没什么好说的&…

QQ账号密码的保存与读取

UserInfopackage com.example.hello;import android.content.Context; import android.content.SharedPreferences;import java.util.HashMap; import java.util.Map;/*** Created by 王林强 on 2018/5/9.*/ //这只是类的描述定义而已&#xff0c;不是申请存储空间&#xff08;…

揭秘QQ 安全密码框的原理

这篇文章也算是朝花夕拾&#xff0c;其实很早以前就知道的原理&#xff0c;现在拿出来和大家交流分享一下。 故事总要有缘由&#xff0c;那么这个故事的缘由就是&#xff0c;当我以前写了一个获取其它进程密码框密码的时候&#xff08;前几篇博客中有描述&#xff09;&#xff…

仿QQ登录、记住密码等功能实现

功能&#xff1a;实现账登录、检查账号密码、记住密码、清除记录功能。 设备&#xff1a;电脑一台、Android Studio软件一个。 技术&#xff1a;xml布局、SharedPreferences的使用。 开始&#xff1a; 1.看效果&#xff1a; 2.文件目录&#xff1a; 3.activity_main.xml 布…

数据库泄露,QQ密码,原来是这样

近期大批数据库被暴&#xff0c;何解呢&#xff1f; 参考相关帖子&#xff0c;加上本人的一点见解&#xff0c;有如下结论 其实也不是什么结论&#xff0c;纯粹就是什么呢&#xff0c;嗯。。。。 “一点都不意外&#xff0c;这在我们圈里流传很久了。”中国鹰派联盟网的创立者…

【Selenium】提高测试爬虫效率:Selenium与多线程的完美结合

前言 使用Selenium 创建多个浏览器&#xff0c;这在自动化操作中非常常见。 而在Python中&#xff0c;使用 Selenium threading 或 Selenium ThreadPoolExecutor 都是很好的实现方法。 应用场景&#xff1a; 创建多个浏览器用于测试或者数据采集&#xff1b;使用Selenium…

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

大家好&#xff0c;我是汤姆凯特。 写在前面&#xff1a;今天用保存QQ账号和密码的实战演练&#xff0c;带大家掌握Android存储中最基本的文件存储方式 文件存储是Android中最基本的一种数据存储方式&#xff0c;它与Java中的文件存储类似&#xff0c;都是通过I/O流形式把数据直…