创建一个登录界面,输入账号密码,点击登录后保存账号密码到手机内部存储空间(如果勾选了保存密码复选框),再次启动读取文件信息将账号密码自动显示出来
代码:
布局:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入账号"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入密码"
android:inputType="textPassword"/>
<CheckBox
android:id="@+id/cb_remember_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:checked="true"/>
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
MainActivity:
package com.suzi.qqlogin;
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.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.suzi.qqlogin.utils.InfoUtil;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
//控件
private EditText et_number = null;
private EditText et_password = null;
private CheckBox cb_remember_password = null;
private Button bt_login = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定控件
bindView();
//回显数据
Map<String,String> infoMap = InfoUtil.GetInfo();
if (infoMap != null)
{
// Toast.makeText(this,"display success",Toast.LENGTH_SHORT).show();
et_number.setText(infoMap.get("number"));
et_password.setText(infoMap.get("password"));
}
}
private void bindView()
{
et_number = (EditText) findViewById(R.id.et_number);
et_password = (EditText) findViewById(R.id.et_password);
cb_remember_password = (CheckBox) findViewById(R.id.cb_remember_password);
bt_login = (Button) findViewById(R.id.bt_login);
bt_login.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
//获取账号密码
String number = et_number.getText().toString().trim();
String password = et_password.getText().toString().trim();
//判断是否为空
if (TextUtils.isEmpty(number) || TextUtils.isEmpty(password))
{
Toast.makeText(this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
//判断是否记住密码
if (cb_remember_password.isChecked())
{
boolean isSaveSuccess = InfoUtil.SaveInfo(number, password);
if (isSaveSuccess)
{
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
} else
{
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
//如果前面的判断没有问题,则登录成功
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}
}
InfoUtil:
package com.suzi.qqlogin.utils;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Suzi on 2016/7/8.
*/
public class InfoUtil
{
/**
* 保存数据
* @param number
* @param password
* @return
*/
public static boolean SaveInfo(String number,String password)
{
//初始化数据
String info = number + "##" + password;
try
{
//存储的路径
String path = "/data/data/com.suzi.qqlogin/qqlogin.txt";
FileOutputStream fos = new FileOutputStream(path);
fos.write(info.getBytes());
fos.flush();
fos.close();
return true;
} catch (Exception e)
{
e.printStackTrace();
}
return false;
}
/**
* 读取数据
* @return
*/
public static Map<String,String> GetInfo()
{
try
{
//存储的路径
String path = "/data/data/com.suzi.qqlogin/qqlogin.txt";
FileInputStream fis = new FileInputStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String info = reader.readLine();
if (!TextUtils.isEmpty(info))
{
String[] split = info.split("##");
Map<String,String> infoMap = new HashMap<String,String>();
infoMap.put("number",split[0]);
infoMap.put("password",split[1]);
return infoMap;
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
效果图:
图1 首次启动
图2 输入账号密码,点击登录,再次启动