保存数据到手机内存代码优化(QQ登录保存密码)

package com.itheima28.qqlogin;

import java.io.File;
import java.util.Map;

import com.itheima28.qqlogin.utils.Utils;
import com.itheima28.qqlogin.utils.UtilsOfSDCard;
import com.itheima28.qqlogin.R;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
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 implements OnClickListener {

	private static final String TAG = "MainActivity";
	private EditText etNumber;
	private EditText etPassword;
	private CheckBox cbRemerberPWD;
	private Map<String, String> userInfoMap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		//  /data/data/包名/files
		/*
		this.getFilesDir-- /data/data/com.itheima28.qqlogin/files
		 */
		File file = this.getFilesDir();
		
		System.out.println(file.toString());
		
		etNumber = (EditText) findViewById(R.id.et_number);
		etPassword = (EditText) findViewById(R.id.et_password);
  
		cbRemerberPWD = (CheckBox) findViewById(R.id.cb_remember_pwd);
		Button btnLogin = (Button) findViewById(R.id.btn_login);
	
	
		btnLogin.setOnClickListener(this);
		
		//回显
		userInfoMap = Utils.getUserInfo(this);
		if(userInfoMap != null){
			etNumber.setText(userInfoMap.get("number"));
			etPassword.setText(userInfoMap.get("password"));
		} 
		
	}

	@Override
	public void onClick(View v) {
		//执行登录的方法
		
		//1.取出号码和密码
		String number = etNumber.getText().toString();
		String password = etPassword.getText().toString();
		
		
		
		if(TextUtils.isEmpty(number)||TextUtils.isEmpty(password)){
			//Toast--弹出吐司,下面的业务就不执行了
			Toast.makeText(this, "请正确输入", Toast.LENGTH_SHORT).show();
			/*
			 第一个参数为Context对象,MainActivity继承Context,所以用this就行了
			第二个参数时候提示的信息
			第三个参数持续的时间   Toast.LENGTH_SHORT--0秒    Toast.LENGTH_LONG--1秒
			 */
		}
		
		//2.判断记住密码是否被选中,如果被选中,存起来
		if(cbRemerberPWD.isChecked()){
			//当前需要记住密码
			Log.i(TAG, "记住密码:"+number + ","+password);
			
			boolean isSuccess = Utils.saveUserInfo(this,number, password);
			if(isSuccess){
				 Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
			}else{
				 Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
			}
		} 
		
		//3.登录成功
		Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
	
	}

	 
}


package com.itheima28.qqlogin.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.text.TextUtils;

public class Utils {
	
	
	
	/*
	 * 保存存户信息
	 * */
	public static boolean saveUserInfo(String number,String password){
		
		try {
			
			String path = "/data/data/com.itheima28.qqlogin/itheima28.txt";
			
			FileOutputStream fos = new FileOutputStream(path);
		
			//存储12345##6545564
			String date	=  number + "##" + password;
			
			fos.write(date.getBytes());
			
			fos.flush();
			
			fos.close();
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
	
	public static boolean saveUserInfo(Context context,String number,String password){
		
		try {
//			String path = "/data/data/com.itheima28.qqlogin/itheima28.txt";
			
			
			File filesDir = context.getFilesDir();
			
			File f = new File(filesDir,"itheima28.txt");
			
			FileOutputStream fos = new FileOutputStream(f);
		
			//存储12345##6545564
			String date	=  number + "##" + password;
			
			fos.write(date.getBytes());
			
			fos.flush();
			
			fos.close();
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
	/**
	 * 返回用户信息
	 * */
	public static Map<String,String> getUserInfo(){
		
		try {
			String path = "/data/data/com.itheima28.qqlogin/itheima28.txt";
			FileInputStream fis = new FileInputStream(path);
			
			//字符流对象
			BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
			
			String text = reader.readLine();
			
			if(TextUtils.isEmpty(text)){
				String[] split = text.split("##");
				
				Map<String,String> userInfoMap = new HashMap<String, String>();
				userInfoMap.put("number", split[0]);
				userInfoMap.put("number", split[1]);
				return userInfoMap;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	
	public static Map<String,String> getUserInfo(Context context){
		
		try {
//			String path = "/data/data/com.itheima28.qqlogin/itheima28.txt";
		
			File filesDir = context.getFilesDir();
			
			File f = new File(filesDir,"itheima28.txt");
			
			FileInputStream fis = new FileInputStream(f);
			
			//字符流对象
			BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
			
			String text = reader.readLine();
			
			if(TextUtils.isEmpty(text)){
				String[] split = text.split("##");
				
				Map<String,String> userInfoMap = new HashMap<String, String>();
				userInfoMap.put("number", split[0]);
				userInfoMap.put("number", split[1]);
				return userInfoMap;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

<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_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入QQ号"
        />
     <EditText 
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="请输入密码"/>
    
     <CheckBox 
         android:id="@+id/cb_remember_pwd"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:checked="true"
         android:text="记住密码"/>
    	 <!-- checkedtrue代表选中 -->  
     
     <Button 
         android:id="@+id/btn_login"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="登录"/>
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值