<span style="font-size:18px;">package com.melody.savepasswd;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import com.melody.savepasswd.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_name;
private EditText et_passwd;
private CheckBox cb;
private FileOutputStream fos;
private FileInputStream fis;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
readAccount();
}
//读取进度方法
private void readAccount() {
//读取文件,回显数据
// File file = new File("data/data/"+getPackageName()+"/login.txt");
File file = new File(getCacheDir(),"login.txt");
if(file.exists()){
try {
fis = new FileInputStream(file);
//把字节流转换成字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//读出来的数据存入到文本 一行一行读
String text = br.readLine();
//回显数据 split 字符切割
String s[] = text.split("&&");
//给输入框设置文本
et_name.setText(s[0]);
et_passwd.setText(s[1]);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
private void init() {
et_name = (EditText) findViewById(R.id.et_name);
et_passwd = (EditText) findViewById(R.id.et_passwd);
cb = (CheckBox) findViewById(R.id.cb);
}
public void login(View v){
//获取用户名和密码
String name = et_name.getText().toString();
String passwd = et_passwd.getText().toString();
//判断复选框是否选中
if(cb.isChecked()){
//创建一个文件保存用户名和密码 手机内部存储路径 data/data/包名/文件名
// File file = new File("data/data/"+getPackageName()+"/login.txt");
//返回一个File对象,封装路径是 data/data/包名/files = getFilesDir()
//File file = new File(getFilesDir(),"login.txt");
//返回一个File对象,封装路径是 data/data/包名/cache = getCacheDir()
File file = new File(getCacheDir(),"login.txt");
try {
fos = new FileOutputStream(file);
//把帐号和密码写如文件
fos.write((name+"&&"+passwd).getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Toast.makeText(this, "登录成功", 0).show();
}
}
</span>
//布局文件/
<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">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入帐号" />
<EditText
android:id="@+id/et_passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<span style="white-space:pre"> </span>android:hint="请输入密码" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<span style="white-space:pre"> </span> <CheckBox
<span style="white-space:pre"> </span> android:id="@+id/cb"
<span style="white-space:pre"> </span> android:layout_width="wrap_content"
<span style="white-space:pre"> </span> android:layout_height="wrap_content"
<span style="white-space:pre"> </span> android:text="记住帐号和密码"
<span style="white-space:pre"> </span> android:layout_centerVertical="true"
<span style="white-space:pre"> </span> />
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span> <Button
<span style="white-space:pre"> </span> android:layout_width="wrap_content"
<span style="white-space:pre"> </span> android:layout_height="wrap_content"
<span style="white-space:pre"> </span> android:layout_alignParentRight="true"
<span style="white-space:pre"> </span> android:onClick="login"
<span style="white-space:pre"> </span> android:text="登录"/>
</RelativeLayout>
</LinearLayout>