Android为数据存储提供了以下几种方式:

1.文件存储方式

2.SharedPreference存储方式

3.Content Provider 内容提供者

4.网络存储方式


(一).文件操作方式(其本质即为输入输出流的操作)

实现登录界面账号、密码的保存功能。

Android 用户数据存储位置 android存储用户信息_android



1.写布局文件,分别添加:

a.图片控件(ImageView)


b.两个输入框(EditText)


c. 单选框(CheckBox)


d. 登录按钮(Button)


<span style="font-size:14px;"><LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.weimitechnology.QQLogin.MainActivity" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    
	<LinearLayout 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal"
	    android:padding="20dip">
	    
    	<ImageView
        android:layout_width="92dp"
        android:layout_height="match_parent"
        android:src="@drawable/f25" />
   
	    <LinearLayout 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:orientation="vertical">
	
	    <EditText
	        android:id="@+id/QQ_number"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content" 
	        android:hint="请输入QQ号码"/>
	    
	    <!-- 密码不能以明文方式显示 -->
	    <EditText
	        android:id="@+id/QQ_password"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:inputType="textPassword"
	        android:hint="请输入QQ密码"/>
   		</LinearLayout>
    </LinearLayout>
    
    <CheckBox 
    	android:id="@+id/QQChecked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:layout_gravity="center"
        android:text="记住密码"/>
    
    <Button 
        android:id="@+id/QQLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_gravity="center_horizontal"
        android:text="登录"/>
    
</LinearLayout></span>


2.构建一个工具类(其中两个函数,分别实现登信息的保存、恢复)


a. 函数一: public static boolean saveUserInfo(String number, String password)


参数 number:传入输入框中的账号信息

参数 password: 传入输入框中的密码信息

备注;当用户选择保存密码,且登录成功后,保存用户登录信息,下次打开应用时自动返回登录账号密码。


b.函数二: public static Map<String, String> getUserInfor()

返回类型:Map返回用户账号密码信息

备注:OnCreate(Bundle savedInstanceState)中调用




package com.weimitechnology.QQLogin.utils;

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

import android.text.TextUtils;
import android.util.Log;

public class Utills {

	/* 函数一:保存账号 密码 */
	public static boolean saveUserInfo(String number, String password){
		
		//  将数据写入文件的步骤:
		try { // 1.指定文件路径
			String path = "/data/data/com.weimitechnology.QQLogin/QQLogin.txt";
			FileOutputStream fosFileOutputStream = new FileOutputStream(path);
			
			// 2.给出所要写入的内容 54321##123456
			String data = number + "##" + password;
			
			// 3.以字节流的方式写入文件
			fosFileOutputStream.write(data.getBytes());
			
			// 4.刷新数据
			fosFileOutputStream.flush();
			
			// 5.关闭数据流
			fosFileOutputStream.close();
			
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return false;		
	}
	
	
	/*函数二:账号密码信息的恢复*/
	public static Map<String, String> getUserInfor(){
		
		// 读取数据的步骤:
		try {
			// 1.获取文件存储路径
			String path = "/data/data/com.weimitechnology.QQLogin/QQLogin.txt";
			FileInputStream fis = new FileInputStream(path);
			
			// 2.构建了一个字符流
			BufferedReader reader= new BufferedReader(new InputStreamReader(fis));
			
			// 3.获取文本信息
			String text = reader.readLine();
			
			// 4.对文本信息进行分割解析
			if(!TextUtils.isEmpty(text))
			{
				String[] spit = text.split("##");
				Map<String, String> userInforMap = new HashMap<String, String>();
				Log.d("getUserInfor()",spit[0]+";"+spit[1]);
				userInforMap.put("number", spit[0]);
				userInforMap.put("password",spit[1]);
				fis.close();
				return userInforMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}




文件操作归纳:


(1).写入文本信息

1.定义写入路径并获得输出流

String path = "/data/data/包名/文件名"

FileOutputStream fos = new FileOutputStream(path);

2.获取所需写入的数据

String data = number + "##" + password;

3.写入数据

fos.write(data.getBytes());

4.刷新数据,关闭输入输出流

fos.flush();

fos.close();


(2).读取文本信息

1.指定路径并获得输入流
String path = "/data/data/包名/文件名"
FileInputStream fis = new FileItputStream(path);
2.获取字符流对象
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
3.读取文本信息
String text = reader.readLine();
4.关闭输入输出流
fis.close();


(3).对读取数据进行解析

从文本中所读取的数据 text = "98765##43210",中间的 ## 为写入文本信息是添加的分隔符:
if(!TextUtils.isEmpty(text)) // 如果所读取道到得文本信息不为空
{ 
String[] spit = text.split("##"); // 对文本中 ## 信息进行分割
Map<String, String> userInforMap = new HashMap<String, String>(); // 新建Map<String, String> 对象
Log.d("getUserInfor()",spit[0]+";"+spit[1]);
userInforMap.put("number", spit[0]); // key-values对应,spit[0]中存放的是账号信息
userInforMap.put("password",spit[1]) ;// key-values对应,spit[1]中存放的是密码信息
fis.close();
return userInforMap;
}



3.Activity中实现登录及信息保存、回显


package com.weimitechnology.QQLogin;

import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
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;

import com.weimitechnology.QQLogin.utils.Utills;


public class MainActivity extends Activity implements OnClickListener {

    private static final String TAG = "MainActivity";
	private EditText qNumber;
	private EditText qPassword;
	private CheckBox qCheck;
	private Button qLogin;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
/*        // 去除标题栏,必须在setContentView之前调用
        requestWindowFeature(Window.FEATURE_NO_TITLE);*/
        
        setContentView(R.layout.activity_main);
        
        qNumber = (EditText) findViewById(R.id.QQ_number);
        qPassword = (EditText) findViewById(R.id.QQ_password);
        qCheck = (CheckBox) findViewById(R.id.QQChecked);
        qLogin = (Button) findViewById(R.id.QQLogin);
    
        qLogin.setOnClickListener(this);
        
        // 回显数据
        Map<String, String> userInforMap = Utills.getUserInfor();
        if(userInforMap != null){
        	Log.d(TAG," 显示账号、密码:" + userInforMap.get("number") +"," + userInforMap.get("password"));
        	qNumber.setText(userInforMap.get("number"));
        	qPassword.setText(userInforMap.get("password"));
        	}
    }

	@Override
	public void onClick(View v) {
		// 执行登录操作
		
		// 1.取出除账号和密码
		String QQnumber = qNumber.getText().toString();
		String QQpassword = qPassword.getText().toString();
		if(TextUtils.isEmpty(QQnumber) || TextUtils.isEmpty(QQpassword))
		{
			Toast.makeText(this, "账号、密码不能为空", Toast.LENGTH_SHORT).show();
			return;
		}
		
		// 2.判断记住密码是否被选中,如果被选中存储起来
		if(qCheck.isChecked())
		{
			Log.d(TAG,"记住密码:" + QQnumber +"," + QQpassword);
			boolean isSuccess = Utills.saveUserInfo(QQnumber, QQpassword);
			if(isSuccess){
				Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT);
			}else{
				Toast.makeText(this, "保存失败",Toast.LENGTH_SHORT);
			}
			
		}
		
		// 3.登录成功
		Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
	}
}



备注(记得复习):按钮点击功能一共有4种实现方式