功能完善的 Android 日历打卡应用

本教程将带你创建一个功能更完善的 Android 日历打卡应用。以下是完整的代码:

**MainActivity.java:**javaimport androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.CalendarView;import android.widget.Toast;import java.text.SimpleDateFormat;import java.util.Date;

public class MainActivity extends AppCompatActivity {

private CalendarView calendarView;    private Button checkInButton;    private String lastCheckedDate;

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

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

    // 设置日历选择监听器        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {            @Override            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {                // 在这里处理选择日期的逻辑                String selectedDate = formatDate(year, month, dayOfMonth);                if (selectedDate.equals(lastCheckedDate)) {                    Toast.makeText(MainActivity.this, '已打卡', Toast.LENGTH_SHORT).show();                } else {                    Toast.makeText(MainActivity.this, '未打卡', Toast.LENGTH_SHORT).show();                }            }        });

    // 设置打卡按钮点击监听器        checkInButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // 在这里处理打卡逻辑                lastCheckedDate = getCurrentDate();                Toast.makeText(MainActivity.this, '打卡成功!', Toast.LENGTH_SHORT).show();            }        });    }

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

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);    }}

**activity_main.xml:**xml

<CalendarView        android:id='@+id/calendarView'        android:layout_width='match_parent'        android:layout_height='wrap_content' />

<Button        android:id='@+id/checkInButton'        android:layout_width='wrap_content'        android:layout_height='wrap_content'        android:text='打卡' />

功能说明:

  • 应用界面包含一个日历视图和一个“打卡”按钮。* 用户可以选择日期,应用会显示该日期是否已打卡。* 点击“打卡”按钮会记录当前日期为已打卡。* lastCheckedDate 变量用于存储上次打卡的日期。

希望这个教程能帮助你构建自己的 Android 日历打卡应用!

Android 日历打卡应用:功能增强版

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

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