Android Studio Java将输入存储在一个文本文件中,所有输入在一行中加载后显示在屏幕上
我正在制作一个体重跟踪应用程序。该应用程序要求用户输入一个按钮来保存重量。单击按钮后,输入将保存到文本文件中。然后有一个加载函数,它在emulator屏幕的下部显示文本文件的内容
我已经设法让所有这些工作,但一旦加载按钮被点击的文本文件内容都显示在一行。例如,908988等,但我想他们被显示在一个新的行,每一个或在他们之间有一些空间
有人能给我指出正确的方向吗? 提前谢谢
主要活动的代码:
package com.example.workouttracker;
import 安卓x.appcompat.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.EditText;
import 安卓.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class WeightTracking extends AppCompatActivity {
private static final String FILE_NAME = "WeightTracking.txt";
EditText mEditText;
EditText mEditText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weight_tracking);
mEditText = findViewById(R.id.weight);
mEditText2 = findViewById(R.id.weight2);
}
public void save(View v) {
String text = mEditText.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(FILE_NAME, MODE_APPEND);
fos.write(text.getBytes());
mEditText.getText().clear();
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME,
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void load(View v) {
FileInputStream fis = null;
try {
fis = openFileInput(FILE_NAME);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String text;
while ((text = br.readLine()) != null) {
sb.append(text).append("\n");
}
mEditText2.setText(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".WeightTracking">
<TextView
安卓:id="@+id/heading"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Weight Tracking"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="center_horizontal"
安卓:layout_marginTop="50dp"
安卓:gravity="center_horizontal"
安卓:text="Please Enter weight in kg's"
安卓:textSize="20sp" />
<EditText
安卓:id="@+id/weight"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="center_horizontal"
安卓:layout_marginTop="10dp"
安卓:ems="6"
安卓:inputType="number|numberDecimal"
安卓:textSize="20sp"/>
<EditText
安卓:id="@+id/weight2"
安卓:layout_width="match_parent"
安卓:layout_height="146dp"
安卓:layout_gravity="center_horizontal"
安卓:layout_marginTop="300dp"
安卓:ems="6"
安卓:inputType="number|numberDecimal"
安卓:textSize="20sp" />
<Button
安卓:id="@+id/button_save"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_centerHorizontal="true"
安卓:layout_gravity="center_horizontal"
安卓:layout_marginTop="150dp"
安卓:text="Save"
安卓:onClick="save"/>
<Button
安卓:id="@+id/button_load"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="center_horizontal"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="220dp"
安卓:text="Load"
安卓:onClick="load"/>
</RelativeLayout>
共 (0) 个答案