一、效果图
本文提供了 Android 自带日期选择器(DatePicker和DatePickerDialog)、时间选择器(TimePicker和TimePickerDialog)的简单使用方法, 完整代码收录在GitHup项目中,
地址: https://github.com/GinGod/BasisDependency ,相关类 BasisTimesUtils.java 和DateTimePickerActivity.java ,
因使用比较简单, 本文只是给出效果图和工具类代码,具体逻辑可自行参考注释理解。
具体效果图如下:
1、年月日选择器
2.年月选择器
3、月日选择器
4、时间选择器
5、嵌套在布局中
二、使用工具类
提供了一个工具类, 里面包含了 DatePickerDialog 和 TimePickerDialog 的简单使用, 可自定义设置背景, 标题, 不显示年或日等package com.gingold.basislibrary.utils;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.NumberPicker;
import android.widget.TimePicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* time 工具类
*/
public class BasisTimesUtils {
public static int THEME_DEVICE_DEFAULT_LIGHT = AlertDialog.THEME_DEVICE_DEFAULT_LIGHT;
public static int THEME_DEVICE_DEFAULT_DARK = AlertDialog.THEME_DEVICE_DEFAULT_DARK;
public static int THEME_TRADITIONAL = AlertDialog.THEME_TRADITIONAL;
public static int THEME_HOLO_LIGHT = AlertDialog.THEME_HOLO_LIGHT;
public static int THEME_HOLO_DARK = AlertDialog.THEME_HOLO_DARK;
private static DatePickerDialog mDatePickerDialog;//日期选择器
/**
* 将字符串时间转为Long时间
*
* @param time yyyy-MM-dd HH:mm:ss:SSS
*/
public static Long getLongTimeOfSSS(String time) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
Date date = sdf.parse(time);
return date.getTime();
} catch (Exception e) {
}
return 0L;
}
/**
* 将字符串时间转为Long时间
*
* @param time yyyy-MM-dd HH:mm:ss
*/
public static Long getLongTime(String time) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(time);
return date.getTime();
} catch (Exception e) {
}
return 0L;
}
/**
* 将字符串时间转为Long时间
*
* @param time yyyy-MM-dd
*/
public static Long getLongTimeOfYMD(String time) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(time);
return date.getTime();
} catch (Exception e) {
}
return 0L;
}
/**
* 将Long时间转成String时间
*
* @return yyyy-MM-dd HH:mm:ss:SSS
*/
public static String getStringTimeOfSSS(Long time) {
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
return sdf.format(date);
}
/**
* 将Long时间转成String时间
*
* @return yyyy-MM-dd HH:mm:ss
*/
public static String getStringTime(Long time) {
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
/**
* 将Long时间转成String时间
*
* @return yyyy-MM-dd
*/
public static String getStringTimeOfYMD(Long time) {
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
/**
* 当前的时间(设备)
*
* @return yyyy-MM-dd HH:mm:ss:SSS
*/
public static String getDeviceTimeOfSSS() {
String date = "";
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
date = df.format(new Date());
} catch (Exception e) {
e.printStackTrace();
date = new Date().getTime() + "";//当前时间的long字符串
}
return date;
}
/**
* 当前的时间(设备)
*
* @return yyyy-MM-dd HH:mm:ss
*/
public static String getDeviceTime() {
String date = "";
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = df.format(new Date());
} catch (Exception e) {
e.printStackTrace();
date = new Date().getTime() + "";//当前时间的long字符串
}
return date;
}
/**
* 当前的时间(年月日)
*
* @return yyyy-MM-dd
*/
public static String getDeviceTimeOfYMD() {
String date = "";
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
date = df.format(new Date());
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* 当前的时间(年月)
*
* @return yyyy-MM
*/
public static String getDeviceTimeOfYM() {
String date = "";
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
date = df.format(new Date());
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* 获取某月最后一天(年月日)
*
* @return yyyy-MM
*/
public static String getLastDayOfMonthOfYMD(int year, int month) {
Calendar cal = Calendar.getInstance();
// 设置年份
cal.set(Calendar.YEAR, year);
// 设置月份
cal.set(Calendar.MONTH, month - 1);
// 获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
// 设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
/**
* 获取某月最后一天(日)
*/
public static int getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
// 设置年份
cal.set(Calendar.YEAR, year);
// 设置月份
cal.set(Calendar.MONTH, month - 1);
// 获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
return lastDay;
}
/**
* 显示日期选择器
*
* @param themeLight true 白色背景; false 黑色背景
*/
public static BasisTimesUtils showDatePickerDialog(Context context, boolean themeLight, String title, int year, int month, int day, OnDatePickerListener onDateTimePickerListener) {
int themeId = AlertDialog.THEME_HOLO_LIGHT;//默认白色背景
if (!themeLight) {
themeId = AlertDialog.THEME_HOLO_DARK;//黑色背景
}
return showDatePickerDialog(context, themeId, title, year, month, day, onDateTimePickerListener);
}
/**
* 显示日期选择器, 默认白色背景
*/
public static BasisTimesUtils showDatePickerDialog(Context context, String title, int year, int month, int day, OnDatePickerListener onDateTimePickerListener) {
return showDatePickerDialog(context, AlertDialog.THEME_HOLO_LIGHT, title, year, month, day, onDateTimePickerListener);
}
/**
* 显示日期选择器
*/
public static BasisTimesUtils showDatePickerDialog(Context context, int themeId, String title, int year, int month, int day,
final OnDatePickerListener onDateTimePickerListener) {
mDatePickerDialog = new DatePickerDialog(context, themeId, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;//月份加一
if (onDateTimePickerListener != null) {
onDateTimePickerListener.onConfirm(year, month, dayOfMonth);
}
}
}, year, month - 1, day);//月份减一
mDatePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (onDateTimePickerListener != null) {
onDateTimePickerListener.onCancel();
}
}
});
if (!TextUtils.isEmpty(title)) {
mDatePickerDialog.setTitle(title);
}
mDatePickerDialog.show();
return new BasisTimesUtils();
}
/**
* 隐藏年, 只显示月和日
*/
public void setYearGone() {
setSpecialDatePicker(1);
}
/**
* 隐藏日, 只显示年和月
*/
public void setDayGone() {
setSpecialDatePicker(2);
}
private void setSpecialDatePicker(int state) {
try {
DatePicker dp = mDatePickerDialog.getDatePicker();
NumberPicker view0 = (NumberPicker) ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(0); //获取最前一位的宽度
NumberPicker view1 = (NumberPicker) ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(1); //获取中间一位的宽度
NumberPicker view2 = (NumberPicker) ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2);//获取最后一位的宽度
//年的最大值为2100
//月的最大值为11
//日的最大值为28,29,30,31
int value0 = view0.getMaxValue();//获取第一个View的最大值
int value1 = view1.getMaxValue();//获取第二个View的最大值
int value2 = view2.getMaxValue();//获取第三个View的最大值
if (state == 1) {//隐藏年, 只显示月和日
if (value0 > 252) {
view0.setVisibility(View.GONE);
} else if (value1 > 252) {
view1.setVisibility(View.GONE);
} else if (value2 > 252) {
view2.setVisibility(View.GONE);
}
} else if (state == 2) {//隐藏日, 只显示年和月
if (value0 > 25 && value0 < 252) {
view0.setVisibility(View.GONE);
} else if (value1 > 25 && value1 < 252) {
view1.setVisibility(View.GONE);
} else if (value2 > 25 && value2 < 252) {
view2.setVisibility(View.GONE);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 显示时间选择器
*/
public static void showTimerPickerDialog(Context context, boolean themeLight, String title, int hourOfDay, int minute, boolean is24HourView, final OnTimerPickerListener onTimerPickerListener) {
int themeId = AlertDialog.THEME_HOLO_LIGHT;//默认白色背景
if (!themeLight) {
themeId = AlertDialog.THEME_HOLO_DARK;//黑色背景
}
showTimerPickerDialog(context, themeId, title, hourOfDay, minute, is24HourView, onTimerPickerListener);
}
/**
* 显示时间选择器, 默认白色背景
*/
public static void showTimerPickerDialog(Context context, String title, int hourOfDay, int minute, boolean is24HourView, final OnTimerPickerListener onTimerPickerListener) {
showTimerPickerDialog(context, AlertDialog.THEME_HOLO_LIGHT, title, hourOfDay, minute, is24HourView, onTimerPickerListener);
}
/**
* 显示时间选择器
*/
public static void showTimerPickerDialog(Context context, int themeId, String title, int hourOfDay, int minute, boolean is24HourView, final OnTimerPickerListener onTimerPickerListener) {
TimePickerDialog dialog = new TimePickerDialog(context, themeId, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (onTimerPickerListener != null) {
onTimerPickerListener.onConfirm(hourOfDay, minute);
}
}
}, hourOfDay, minute, is24HourView);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (onTimerPickerListener != null) {
onTimerPickerListener.onCancel();
}
}
});
if (!TextUtils.isEmpty(title)) {
dialog.setTitle(title);
}
dialog.show();
}
/**
* 日期选择器监听
*/
public interface OnDatePickerListener {
void onConfirm(int year, int month, int dayOfMonth);
void onCancel();
}
/**
* 时间选择器监听
*/
public interface OnTimerPickerListener {
void onConfirm(int hourOfDay, int minute);
void onCancel();
}
}
三、使用方法
提供了几个简便使用的方法/**
* 时间选择
*/
private void showTimerPicker() {
BasisTimesUtils.showTimerPickerDialog(mActivity, true, "请选择时间", 21, 33, true, new BasisTimesUtils.OnTimerPickerListener() {
@Override
public void onConfirm(int hourOfDay, int minute) {
toast(hourOfDay + ":" + minute);
}
@Override
public void onCancel() {
toast("cancle");
}
});
}
/**
* 月日选择
*/
private void showMonthDayPicker() {
BasisTimesUtils.showDatePickerDialog(context, false, "请选择月日", 2015, 8, 28, new BasisTimesUtils.OnDatePickerListener() {
@Override
public void onConfirm(int year, int month, int dayOfMonth) {
toast(year + "-" + month + "-" + dayOfMonth);
}
@Override
public void onCancel() {
toast("cancle");
}
}).setYearGone();
}
/**
* 年月选择
*/
private void showYearMonthPicker() {
BasisTimesUtils.showDatePickerDialog(context, true, "", 2015, 12, 22,
new BasisTimesUtils.OnDatePickerListener() {
@Override
public void onConfirm(int year, int month, int dayOfMonth) {
toast(year + "-" + month + "-" + dayOfMonth);
}
@Override
public void onCancel() {
toast("cancle");
}
}).setDayGone();
}
/**
* 年月日选择
*/
private void showYearMonthDayPicker() {
BasisTimesUtils.showDatePickerDialog(context, BasisTimesUtils.THEME_HOLO_DARK, "请选择年月日", 2015, 1, 1, new BasisTimesUtils.OnDatePickerListener() {
@Override
public void onConfirm(int year, int month, int dayOfMonth) {
toast(year + "-" + month + "-" + dayOfMonth);
}
@Override
public void onCancel() {
toast("cancle");
}
});
}
四、在布局中使用DatePicker和TimePicker
1、布局文件如下, 需要添加属性: android:theme="@android:style/Theme.Holo.Light"
android:id="@+id/dp_datetimepicker_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:theme="@android:style/Theme.Holo.Light">
android:id="@+id/tp_datetimepicker_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:theme="@android:style/Theme.Holo.Light">
2、代码中设置变化监听//日期选择器初始化和变化监听
dp_datetimepicker_date.init(2016, 1, 1, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
BasisLogUtils.e("onDateChanged: " + year + "-" + monthOfYear + "-" + dayOfMonth);
}
});
//时间选择器初始化和变化监听
tp_datetimepicker_time.setIs24HourView(true);
tp_datetimepicker_time.setHour(16);
tp_datetimepicker_time.setMinute(36);
tp_datetimepicker_time.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
BasisLogUtils.e(hourOfDay + ":" + minute);
}
});
五、总结
1、DatePicker和TimePicker是系统自带的选择器, 使用比较简单, 基本可满足对选择器没特殊要求的日常开发
2、高版本提供了新的显示方式, 可通过设置主题为 THEME_DEVICE_DEFAULT_LIGHT 和 THEME_DEVICE_DEFAULT_DARK 查看效果, 大家可自行尝试
六、关联
完整代码收录在GitHup项目中,地址: https://github.com/GinGod/BasisDependency ,相关类 BasisTimesUtils.java 和DateTimePickerActivity.java