android日程提醒完整的java.class代码和布局文件
时间: 2023-08-22 18:07:12 浏览: 109
以下是一个用于创建日程提醒的简单Android应用程序的完整Java类和布局文件示例。
布局文件schedule_reminder.xml:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/title_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="提醒标题" />
<EditText
android:id="@+id/desc_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title_edittext"
android:layout_margin="16dp"
android:hint="提醒描述" />
<TextView
android:id="@+id/time_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/desc_edittext"
android:layout_margin="16dp"
android:text="选择提醒时间" />
<Button
android:id="@+id/select_time_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/time_textview"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="选择时间" />
<Button
android:id="@+id/add_reminder_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/select_time_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="添加提醒" />
</RelativeLayout>
```
Java类ScheduleReminderActivity.java:
```java
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class ScheduleReminderActivity extends AppCompatActivity {
private EditText titleEditText, descEditText;
private TextView timeTextView;
private Button selectTimeButton, addReminderButton;
private int hour, minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule_reminder);
titleEditText = findViewById(R.id.title_edittext);
descEditText = findViewById(R.id.desc_edittext);
timeTextView = findViewById(R.id.time_textview);
selectTimeButton = findViewById(R.id.select_time_button);
addReminderButton = findViewById(R.id.add_reminder_button);
selectTimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showTimePicker();
}
});
addReminderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addReminder();
}
});
}
private void showTimePicker() {
final Calendar calendar = Calendar.getInstance();
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(ScheduleReminderActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
timeTextView.setText(String.format("%02d:%02d", hour, minute));
}
}, hour, minute, true);
timePickerDialog.show();
}
private void addReminder() {
String title = titleEditText.getText().toString();
String desc = descEditText.getText().toString();
if (title.isEmpty() || desc.isEmpty()) {
Toast.makeText(this, "请填写提醒标题和描述", Toast.LENGTH_SHORT).show();
return;
}
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(ScheduleReminderActivity.this, ReminderReceiver.class);
intent.putExtra("title", title);
intent.putExtra("desc", desc);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ScheduleReminderActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(this, "提醒已设置", Toast.LENGTH_SHORT).show();
}
}
```
该应用程序包含一个布局文件和一个Java类。布局文件包含一些文本框和按钮,用于输入提醒标题、描述和时间,并添加提醒。Java类包含与布局文件中的控件交互的代码,并使用AlarmManager和BroadcastReceiver来触发提醒。请注意,还需要创建一个ReminderReceiver类,以便在提醒触发时通知用户。
阅读全文