单击按钮时,将时间保存到文本文件

When button is clicked, save the time to a text file

所以我想在 Android Studio 中构建一个应用程序来帮助我的家人和我戒烟。我希望应用程序在单击按钮时保存当前时间。然后我希望应用程序显示 "the last time you smoked" 的时间以及所有之前的 "last time you smoked" 的时间。我是 java 的新手,谁能帮助我或指导我的方向?

为此使用 SharedPreferences,如下所示:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

Editor editor = preferences.edit();
editor.putString("LAST_SMOKE_DATE", new Date().toString());
editor.apply();

要恢复值,请使用:

String dateStr = preferences.getString("LAST_SMOKE_DATE", "");

您可以按照 Whosebug 上的一些教程格式化您的日期:

Change date format in a Java string

我推荐 SQLiteOpenHelper。

SQLiteOpenHelper Android Documentation

每次按下按钮时,都会向其中插入一条新记录。当您希望它显示时,使用游标对象检索记录。