首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按钮在android basic apo中不起作用我正在尝试创建

按钮在android basic apo中不起作用我正在尝试创建
EN

Stack Overflow用户
提问于 2019-11-13 05:18:01
回答 1查看 23关注 0票数 0

我的第一个应用出现了问题。当我点击“提交”时,它的意思是用用户输入的值创建一个文本文件,但它并没有创建它。我不确定问题出在哪里。

我还想知道是否有更好的方法来存储用户输入的数据,使其最终在excel电子表格中可用。或者仅仅使用纯文本文件来使其更容易?

代码语言:javascript
复制
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();
                        }
                    }
                }
            }
        });
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-11-13 05:43:53

柯林斯

如何在android中读/写文本文件的问题已经在这个帖子中得到了回答:

How can I read a text file in Android?

然而,这并不是你在android中想要的。首先,您应该熟悉应用程序和服务的概念。然后,为了解决你的问题:

  • 适用于较少的数据量(例如设置)您可以使用SharedPreferences.
  • For more data (设置更多数据)。记录)推荐的方法是具有SQLite数据库的ContentProvider。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58826816

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档