一、DatePicker和TimePicker的两种监听器之OnDateChangedListener与OnTimeChangedListener
当用户更改了DatePicker(TimePicker)里的年、月、日(时、分)时,将触发OnDateChangedListener(OnTimeChangedListener)监听器的onDateChanged() (onTimeChanged())事件。
示例:
datePicker=(DatePicker)findViewByID(R.id.datePicker);
//datePicker初始化
//Override
datePicker.init(year,month,day,new OnDateChangedListener(){
public void onDateChanged(DataPicker view,int year,int monthOfYear,int dayOfMonth)
{
}
});
timePicker=(TimePicker)findViewByID(R.id.timePicker);
public void setOnTimeChangedListener(new OnTimeChangedListener(){
//Override
public void onTimeChanged(TimePicker view,int hourOfDay,int minute)
{
}
});
二、DatePicker和TimePicker的两种监听器之
OnDateSetListener与OnTimeSetListener
当用户更改了DatePickerDialog(TimePickerDialog)里的年、月、日(时、分)时,将触发OnDateSetListener(OnTimeSetListener)监听器的onDateSet() (onTimeSet())事件。
以对话框的形式展示日期选择器和时间选择器(DatePickerDialog、TimePickerDialog——show()方法用来显示)
如:new DatePickerDialog(this,new OnDateSetListener(){ ...}).show();
三、Calendar
1.获取Calendar的对象
Calendar cal=Calendar.getInstance();
2.获取年月日时分秒的信息
int year=cal.get(Calendar.YEAR);
int month=cal.get(Calendar.MONTH)+1; //Calendar中1月从0开始记
int day=cal.get(Calendar.DAY_OF_MONTH);
int hour=cal.get(Calendar.HOUR_OF_DAY);
int minute=cal.get(Calendar.MINUTE);