Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right
Table of contents
3. Obtain interface data and judge whether the data is empty
4. Obtain the internal storage path
5. Store data in a specific format
6. Echo data every time the interface starts
foreword
0 Basics of Android development, I learned internal storage knowledge today, I want to record it so that I can review it better in the future. In order to better interpret this knowledge, let me give you an example to illustrate: When we log in to QQ, we usually encounter a function of remembering passwords, which can be implemented with internal storage. The realization idea is as follows
1. Write a QQ login interface
The activity_main.xml file code and effect diagram are as follows
<?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. Register the control
Register controls and click events in the onCreate method
3. Obtain interface data and judge whether the data is empty
The handlerLoginEvent(v) function is triggered by the previous step click event
4. Obtain the internal storage path
The method of obtaining the cache file storage path: getCacheDir()
The method of obtaining the common file storage path: getFilesDir()
5. Store data in a specific format
First, a file info.text of File type is newly created under the previously acquired path, and then the write() method of the FileOutputStream class is used to write data.
6. Echo data every time the interface starts
When we reopen the page every time, the page will display the data saved last time, so we need to write the code to display the data in the onResume() method of activity. According to the life cycle of the activity, onResume will be started every time the page starts ( )method. First use the openFileInput() method of the FileInputStream class to open the specified file info.text, and then use the readLine() method of the BufferedReader class to read the data.
7. Attach the code at the end
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);
}
}