我的第一个应用出现了问题。当我点击“提交”时,它的意思是用用户输入的值创建一个文本文件,但它并没有创建它。我不确定问题出在哪里。
我还想知道是否有更好的方法来存储用户输入的数据,使其最终在excel电子表格中可用。或者仅仅使用纯文本文件来使其更容易?
package com.example.mybodyrecord;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
String myBodyRecord = "My_Body_Record.txt";
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
File file; // declare here and initialize inside onCreate.
Button button;
EditText weightInput;
EditText ketoneBloodInput;
EditText ketoneUrineInput;
EditText bloodPressureInput;
EditText bloodSugarInput;
String weight;
String ketoneBlood;
String ketoneUrine;
String bloodPressure;
String bloodSugar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Setting where to get the data from, ie what fields
weightInput = findViewById(R.id.weight);
ketoneBloodInput = findViewById(R.id.ketoneblood);
ketoneUrineInput = findViewById(R.id.ketoneurine);
bloodPressureInput = findViewById(R.id.bp);
bloodSugarInput = findViewById(R.id.bslvl);
button = findViewById(R.id.submit);
//activity has a context now you can use getApplicationContext or this
file = new File(this.getFilesDir(),myBodyRecord);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileOutputStream fos = null;
//Taking input from the fields
weight = weightInput.getText().toString() +"kg";
ketoneBlood = ketoneBloodInput.getText().toString() +"mmol/L";
ketoneUrine = ketoneUrineInput.getText().toString();
bloodPressure = bloodPressureInput.getText().toString() +"mmHg";
bloodSugar = bloodSugarInput.getText().toString() +"mmol/L";
//Save to the file, with the date and time
if (file.exists()) {
try {
fos = openFileOutput(myBodyRecord, getApplicationContext().MODE_APPEND);
fos.write("\n\r".getBytes());
fos.write(formatter.format(date).getBytes());
fos.write(weight.getBytes());
fos.write(ketoneBlood.getBytes());
fos.write(ketoneUrine.getBytes());
fos.write(bloodPressure.getBytes());
fos.write(bloodSugar.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
else {
try {
fos = openFileOutput(myBodyRecord, getApplicationContext().MODE_PRIVATE);
fos.write("\n\r".getBytes());
fos.write(formatter.format(date).getBytes());
fos.write(weight.getBytes());
fos.write(ketoneBlood.getBytes());
fos.write(ketoneUrine.getBytes());
fos.write(bloodPressure.getBytes());
fos.write(bloodSugar.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
}发布于 2019-11-13 05:43:53
柯林斯
如何在android中读/写文本文件的问题已经在这个帖子中得到了回答:
How can I read a text file in Android?
然而,这并不是你在android中想要的。首先,您应该熟悉应用程序和服务的概念。然后,为了解决你的问题:
https://stackoverflow.com/questions/58826816
复制相似问题