提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
目录
前言
0基础学Android开发,今天学到了内部存储知识,想以此记录一下,以便以后能更好的复习。为了更好的诠释这个知识,下面我举个例子来给大家说明:当我们登录qq的时候,通常会遇到一个记住密码的功能,此功能就可以用内部存储实现。实现思路如下
1、写一个QQ登录界面
activity_main.xml文件代码和效果图如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@mipmap/background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="30dp"
android:padding="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/icon_qq"
android:text="QQ"
android:textSize="50sp"/>
<EditText
android:id="@+id/et_account"
android:layout_marginTop="25dp"
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:hint="密码"
android:inputType="textPassword"/>
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#8000aaff"
android:text="登录"
android:textSize="20sp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码"
android:textColor="#00aaff"
android:textSize="16sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="新用户注册"
android:textColor="#00aaff"
android:textSize="16sp"/>
</RelativeLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:text="登录即代表阅读并同意阅读条款"
android:textColor="#00aaff"
android:textSize="20sp" />
</RelativeLayout>
2、注册控件
在onCreate方法里注册控件和点击事件
3、获取界面数据并判断数据是否为空
handlerLoginEvent(v)函数由上一步点击事件触发
4、获取内部存储路径
获取缓存文件存储路径的方法:getCacheDir()
获取普通文件存储路径的方法:getFilesDir()
5、以特定的格式来存储数据
首先在前面获取的路径下new了一个File类型的文件info.text,然后使用FileOutputStream类的write()方法写入数据。
6、每次界面启动时回显数据
当我们每次重新打开页面时,页面要显示上次保存的数据,所以我们要在acticity的onResume()方法里写回显数据的代码,根据activity的生命周期,每次页面启动都会启动onResume()方法。首先使用FileInputStream类的openFileInput()方法打开指定文件info.text,然后使用BufferedReader类的readLine()方法读取数据。
7、最后附上代码
package com.example.qqlogindemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private EditText mAccount;
private EditText mPassword;
private Button mLogin;
/**
* 在Android系统中每一个应用就是一个用户,每个用户的权限都是特定的,不可操作其他应用的内容
* 获取保存文件路径的方法:getFilesDir()
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 注册控件
initView();
// 注册点击事件
initListener();
}
@Override
protected void onResume() {
super.onResume();
try {
// 读取文件数据
FileInputStream fileInputStream = this.openFileInput("info.text");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String info = bufferedReader.readLine();
// 解析数据格式
String[] splits = info.split("\\*\\*\\*");
String account = splits[0];
String password = splits[1];
// 回显数据
mAccount.setText(account);
mPassword.setText(password);
}catch (Exception e){
e.printStackTrace();
}
}
private void initListener() {
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handlerLoginEvent(v); //处理登录事件
}
});
}
private void handlerLoginEvent(View v) {
// 获取账号和密码
String accountText = mAccount.getText().toString();
String passwordText = mPassword.getText().toString();
/**
* 对账号和密码进行检查,检查指标:账号长度,敏感词,密码复杂度,是否非空
*/
if (TextUtils.isEmpty(accountText)){
// 当前账号为空
Toast.makeText(this,"账号不能为空",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(passwordText)){
// 当前密码为空
Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
return;
}
// 保存账号和密码
saveUserInfo(accountText,passwordText);
}
private void saveUserInfo(String accountText, String passwordText) {
//Log.d(TAG, "saveUserInfo: 保存用户信息.....");
// 获取当前应用缓存文件存储路径
File cacheDir = this.getCacheDir();
//Log.d(TAG, "saveUserInfo: file dir =====" + cacheDir.toString());
// 获取当前应用普通文件存储路径
File filesDir = this.getFilesDir();
//Log.d(TAG, "saveUserInfo: file dir =====" + filesDir.toString());
try {
File saveFile = new File(filesDir,"info.text");
if (!saveFile.exists()){
saveFile.createNewFile();
}
// 以特定格式来存储用户信息
FileOutputStream fos = new FileOutputStream(saveFile);
fos.write((accountText + "***" + passwordText).getBytes());
fos.close();
Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
}
}
private void initView() {
mAccount = findViewById(R.id.et_account);
mPassword = findViewById(R.id.et_password);
mLogin = findViewById(R.id.bt_login);
}
}