DatePicker和TimePicker两种实现动态输入日期和时间的功能
DatePickerDialog和TimePickerDialog两种实现动态输入日期和时间的对话框
检测日期时间改变的监听器
OnDateChangedLister和OnTimeChangedLister()
当用户改变了DatePicker中的年月日触发OnDateChangedLister监听器的OnDateChange()事件
当用户改变了TimePicker中的年月日触发OnTimeChangedLister监听器的OnTimeChanged()事件
OnDateSetLisener和OnTimeSetLister()
当用户改变了DatePickerDialog中的年月日触发OnDateSetLister监听器的OnDateSet()事件
当用户改变了TimePickerDialog中的年月日触发OnTimeSetLister监听器的OnTimeSet()事件
案例:在标题栏中展示当前日期和时间,通过监听器获取用户选择的日期或时间的结果,并将变更的日期或时间展示在标题栏
package com.example.time;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private DatePicker datePicker;
private TimePicker timePicker;
private Calendar calendar;
private int year;
private int month;
private int day;
private int hour;
private int minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取日历的对象
calendar = Calendar.getInstance();
//获取年月日时分秒 月从0开始计算
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
setTitle(year + "-" + month + "-" + day + "-" + hour + ":" + minute);
datePicker = findViewById(R.id.datePicker);
timePicker = findViewById(R.id.timePIcker);
//datePicker初始化(传入的是初始的日期)
//calender和picker都是用0表示第一个月,所以不用改
datePicker.init(year, day, calendar.get(Calendar.MONTH), new DatePicker.OnDateChangedListener() {
@Override
//传入人为改变的日期
//监听器中最终显示出来一月不能是0,所以监听器都要+1
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
setTitle(year + "-" + (monthOfYear+1) + "-" + dayOfMonth);
}
});
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
setTitle(hourOfDay + ":" + minute);
}
});
// 上下文,监听器,初始化月,初始化年,初始化月,初始化日
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
setTitle(year + "-" + (monthOfYear+1)+ "-" + dayOfMonth);
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),day).show();
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setTitle(hourOfDay + ":" + minute);
}
},hour,minute,true).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TimePicker
android:id="@+id/timePIcker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>