这个问题涉及到两个问题,一个是数据丢失之前保存下来,这里就需要在onDestroy的时候就把数据保存下来。接着就是再把保存下来的东西取出来。
话不多说,咱开始吧。
首先是一个保存的操作,咱给它命名为save():
private void save(String inputText) { FileOutputStream out = null; BufferedWriter writer = null; try { out = openFileOutput("data", MODE_PRIVATE); writer = new BufferedWriter(new OutputStreamWriter(out)); writer.write(inputText); } catch (IOException e) { e.printStackTrace(); }finally { if(writer != null){ try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } }
然后再写一个load(),用于把数据取出:
public String load(){ FileInputStream in = null; BufferedReader reader = null; StringBuilder content = new StringBuilder(); try { in = openFileInput("data"); reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while((line = reader.readLine())!= null){ content.append(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(reader != null) try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } return content.toString(); }
这里我们就搞得七七八八了。现在设计一个巨简单的Layout,里面只放一个EditText:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" tools:context=".MainActivity"> <EditText android:id="@+id/edt" android:layout_width="match_parent" android:layout_height="wrap_content" android:minLines="3" android:maxLines="3" android:hint="Enter Something..." android:gravity="top"/>
接着,完整代码如下:
package com.example.tempdatasaveapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.widget.EditText; import android.widget.Toast; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class MainActivity extends AppCompatActivity { private EditText edt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edt = findViewById(R.id.edt); String inputText = load(); if(!TextUtils.isEmpty(inputText)){ edt.setText(inputText); edt.setSelection(inputText.length()); Toast.makeText(this, "成功!", Toast.LENGTH_SHORT).show(); } } @Override protected void onDestroy() { super.onDestroy(); String inputText = edt.getText().toString(); save(inputText); } private void save(String inputText) { FileOutputStream out = null; BufferedWriter writer = null; try { out = openFileOutput("data", MODE_PRIVATE); writer = new BufferedWriter(new OutputStreamWriter(out)); writer.write(inputText); } catch (IOException e) { e.printStackTrace(); }finally { if(writer != null){ try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } public String load(){ FileInputStream in = null; BufferedReader reader = null; StringBuilder content = new StringBuilder(); try { in = openFileInput("data"); reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while((line = reader.readLine())!= null){ content.append(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(reader != null) try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } return content.toString(); } }
搞定!