import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private CalendarView calendarView;
    private Button checkInButton;

    private AppDatabase appDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        calendarView = findViewById(R.id.calendarView);
        checkInButton = findViewById(R.id.checkInButton);

        appDatabase = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, 'check-in-db')
                .allowMainThreadQueries()
                .build();

        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                String selectedDate = formatDate(year, month, dayOfMonth);
                Toast.makeText(MainActivity.this, '选择的日期:' + selectedDate, Toast.LENGTH_SHORT).show();
            }
        });

        checkInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String currentDate = getCurrentDate();
                String currentTime = getCurrentTime();
                CheckIn checkIn = new CheckIn(currentDate, currentTime);
                appDatabase.checkInDao().insert(checkIn);
                Toast.makeText(MainActivity.this, '打卡成功:' + currentDate + ' ' + currentTime, Toast.LENGTH_SHORT).show();
            }
        });

        int checkInCount = appDatabase.checkInDao().getCheckInCount();
        Toast.makeText(this, '已打卡次数:' + checkInCount, Toast.LENGTH_SHORT).show();

        List<CheckIn> checkIns = appDatabase.checkInDao().getAllCheckIns();
        for (CheckIn checkIn : checkIns) {
            System.out.println('打卡记录:' + checkIn.getDate() + ' ' + checkIn.getTime());
        }
    }

    private String formatDate(int year, int month, int dayOfMonth) {
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd');
        Date date = new Date(year - 1900, month, dayOfMonth);
        return sdf.format(date);
    }

    private String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd');
        Date date = new Date();
        return sdf.format(date);
    }

    private String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat('HH:mm:ss');
        Date date = new Date();
        return sdf.format(date);
    }
}

这段代码展示了如何使用Android Room数据库和CalendarView创建一个简单的打卡应用。

主要功能:

  • 使用CalendarView选择日期
  • 点击按钮进行打卡,并将打卡日期和时间保存到Room数据库
  • 显示已打卡次数
  • 读取并打印所有打卡记录

注意:

  • 这段代码示例假设你已经创建了CheckIn实体类、CheckInDao接口和AppDatabase数据库类。确保这些类的代码也包含在你的项目中。
  • 为了简洁,代码中使用了allowMainThreadQueries()。在实际开发中,建议使用后台线程进行数据库操作,以避免阻塞主线程。

希望这篇文章能帮助你学习如何使用Android Room数据库和CalendarView创建简单的打卡应用!

Android Room数据库实战:打造简易打卡应用

原文地址: https://www.cveoy.top/t/topic/blpp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录