package com.itheima.dengluanli;
import com.itheima.dengluanli.utils.Utils;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_username;
private EditText et_password;
private CheckBox cb_issave;
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
cb_issave = (CheckBox) findViewById(R.id.cb_issave);
button1 = (Button) findViewById(R.id.bt_button1);
//给按钮设置监听事件
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//获取输入的账号和密码
String username = et_password.getText().toString().trim();
String password = et_password.getText().toString().trim();
//判断账号和密码是否为空
if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
//如果为空给出提示
Toast.makeText(MainActivity.this,"账号和密码不能为空", Toast.LENGTH_SHORT).show();
}else{
//判断是否勾选保存
boolean checked = cb_issave.isChecked();
if(checked){
try {
boolean saveInfo = Utils.saveInfo2(MainActivity.this,username,password);
if(saveInfo){
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//如果不为空,提示保存成功
Toast.makeText(MainActivity.this,"保存成功", Toast.LENGTH_SHORT).show();
}
}
}
});
//读取信息
try {
String[] readInfo = Utils.readInfo(MainActivity.this);
if(readInfo!=null){
et_username.setText(readInfo[0]);
et_password.setText(readInfo[1]);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.itheima.dengluanli.utils;
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 android.content.Context;
public class Utils {
public static boolean saveInfo (Context context,String username,String password) throws Exception{
File filesDir = context.getFilesDir();
String info = username+"##"+password;
try {
FileOutputStream fos = new FileOutputStream(filesDir+"/info.txt");
fos.write(info.getBytes());
fos.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public static boolean saveInfo2(Context context,String username,String password) throws Exception{
String info2 = username+"##"+password;
try {
FileOutputStream openFileOutput = context.openFileOutput("info2.txt",Context.MODE_PRIVATE);
openFileOutput.write(info2.getBytes());
openFileOutput.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public static String[] readInfo(Context context) throws Exception{
try {
FileInputStream openFileInput = context.openFileInput("info2.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput));
String line = br.readLine();
String [] arr = line.split("##");
return arr;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}