Android Material风格年月日和时间选择器
程序员文章站
2022-02-16 11:56:19
...
目前正在做一个 手机记帐App、所谓记帐那么时间是必不可少的栏位、因我 DD记帐 的App风格完全是采用Android Material Design开发的、沿用以前的日期选择控件在这里风格不统一、所以我使用了默认的DatePickerDialog和TimePickerDialog、Android 5.0之后界面已经很好看咯、不再像以前那么难看咯、由于目前Andoird 4.4版本的手机已经很少咯、所以个人觉得可以直接采用默认的时间控件、下面是效果图、非常好看
DatePickerDialog实现代码
DatePickerDialog dd = new DatePickerDialog(DateTimePicker.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { try { SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); String dateInString = dayOfMonth "/" (monthOfYear 1) "/" year; Date date = formatter.parse(dateInString); txtdate.setText(formatter.format(date).toString()); formatter = new SimpleDateFormat("dd/MMM/yyyy"); txtdate.setText(txtdate.getText().toString() " " formatter.format(date).toString()); formatter = new SimpleDateFormat("dd-MM-yyyy"); txtdate.setText(txtdate.getText().toString() " " formatter.format(date).toString()); formatter = new SimpleDateFormat("dd.MMM.yyyy"); txtdate.setText(txtdate.getText().toString() " " formatter.format(date).toString()); } catch (Exception ex) { } } }, year, month, day); dd.show();
TimePickerDialog实现代码
TimePickerDialog td = new TimePickerDialog(DateTimePicker.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { try { String dtStart = String.valueOf(hourOfDay) ":" String.valueOf(minute); format = new SimpleDateFormat("HH:mm"); timeValue = new java.sql.Time(format.parse(dtStart).getTime()); txttime.setText(String.valueOf(timeValue)); String amPm = hourOfDay % 12 ":" minute " " ((hourOfDay >= 12) ? "PM" : "AM"); txttime.setText(amPm " " String.valueOf(timeValue)); } catch (Exception ex) { txttime.setText(ex.getMessage().toString()); } } }, hour, min, DateFormat.is24HourFormat(DateTimePicker.this) ); td.show();
Android Material年月日选择器源代码下载链接: Material时间选择 密码: bibs