数据保存在Sd卡,并且账号密码信息保存

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;
	}
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值