做项目中需要做时间选择器代码,把我实现了的时间选择代码给大家参考一下,也许大家会用到
一、日期选择器
放到全局变量
private Calendar showDate;
相当于缓存
showDate=Calendar.getInstance();
按钮的监听
@OnClick(R.id.activity_report_week_form_washout_date_textView) void washoutDateAction() {
showDateDialog();}
TextView显示日期
@BindView(R.id.activity_report_week_form_washout_date_textView) TextView washoutDateTextView;
主要实现功能代码
private void showDateDialog() { new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { showDate.set(Calendar.YEAR,year); showDate.set(Calendar.MONTH,monthOfYear); showDate.set(Calendar.DAY_OF_MONTH,dayOfMonth); washoutDateTextView.setText(DateFormat.format("yyyy-MM-dd",
showDate
)); } },
showDate.get(Calendar.
YEAR),
showDate.get(Calendar.
MONTH),
showDate.get(Calendar.
DAY_OF_MONTH)).show();}
PS:
DatePickerDialog.OnDateSetListener()监听有比较大的区别
二、时间选择器
全局变量
private Calendar showTime;
相当于缓存
showTime=Calendar.getInstance();
按钮的监听
@OnClick(R.id.activity_report_week_form_washout_date_textView) void washoutDateAction() {
showTimeDialog();}
TextView显示日期
@BindView(R.id.activity_report_week_form_washout_date_textView) TextView washoutDateTextView;
主要实现功能代码
private void showTimeDialog(){ new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { showTime.set(Calendar.HOUR_OF_DAY,hourOfDay); showTime.set(Calendar.MINUTE,minute); washoutDateTextView.setText(DateFormat.format("HH:mm",showTime)); } },showTime.get(Calendar.HOUR_OF_DAY),showTime.get(Calendar.MINUTE),true).show(); }
PS:
TimePickerDialog.OnTimeSetListener()监听有比较大的区别
以上俩款时间选择器,也许大家会用到