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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lenovo.myapplication.MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="@drawable/head"/>
<LinearLayout
android: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_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="#ffffff">
<TextView
android: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"/>
<EditText
android: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>
<LinearLayout
android: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_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff">
<TextView
android: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"/>
<EditText
android: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>
<Button
android: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="50dp"
android:background="#3C8DC4"
android:text="登录"
android:textColor="#ffffff"
android:textSize="20sp"/>
</RelativeLayout>
创建一个工具类FlieSaveQQ,在该类中实现存储读取功能
FlieSaveQQ.java
package com.example.lenovo.myapplication;
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, String
password) {
FileOutputStream fos = null;
try {
//获取文件的输出流对象fos
fos = 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 {
//获取文件的输入流对象fis
fis = 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 com.example.lenovo.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化界面
initView();
Map<String ,String > userInfo = FileSaveQQ.getUserInfo(this);
if(userInfo != null){
etNumber.setText(userInfo.get("account"));
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);
}
@Override
public void onClick(View v) {
//当单机登录按钮时,获取QQ账号和密码
String number = etNumber.getText().toString().trim();
String password = etPassword.getText().toString();
//检验账号和密码是否正确
if(TextUtils.isEmpty(number)){
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,number,password);
if(isSaveSuccess){
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
}
}
}