项目中要用到钟表控件,就尝试了一些开源项目。
//日历控件
DatePickerDialog dp = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int iyear, int monthOfYear, int dayOfMonth) {
/* long maxDate = datePicker.getMaxDate();//日历最大能设置的时间的毫秒值
int year = datePicker.getYear();//年
int month = datePicker.getMonth();//月-1
int dayOfMonth1 = datePicker.getDayOfMonth();//日*
//iyear:年,monthOfYear:月-1,dayOfMonth:日
Toast.makeText(getApplicationContext(), iyear +":"+ (monthOfYear+1)+":"+dayOfMonth , Toast.LENGTH_LONG).show();
}
}, 2013, 2, 1);//2013:初始年份,2:初始月份-1 ,1:初始日期
dp.show();
//钟表控件
TimePickerDialog tp = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int iminute) {
/*int minute = timePicker.getMinute();//分钟
int hour = timePicker.getHour();//小时*/
//hourOfDay:小时,iminute分钟
Toast.makeText(getApplicationContext(), hourOfDay +":"+ iminute , Toast.LENGTH_LONG).show();
}
}, 12, 0, true);//12:钟表初始小时数,0:钟表初始分钟数
tp.show();//记得show一下
compile 'cn.qqtheme.framework:WheelPicker:1.4.5' //时间选择器
compile 'cn.qqtheme.framework:FilePicker:1.4.5'
compile 'cn.qqtheme.framework:ColorPicker:1.4.5'
②使用
DateTimePicker picker = new DateTimePicker(this, DateTimePicker.HOUR_24);//24小时值
picker.setDateRangeStart(2017, 1, 1);//日期起点
picker.setDateRangeEnd(2020, 1,1);//日期终点
picker.setTimeRangeStart(0, 0);//时间范围起点
picker.setTimeRangeEnd(23, 59);//时间范围终点
picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
@Override
public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
//year:年,month:月,day:日,hour:时,minute:分
Toast.makeText(getApplicationContext(), year + "-" + month + "-" + day + " "
+ hour + ":" + minute, Toast.LENGTH_LONG).show();
}
});
picker.show();
compile 'com.contrarywind:Android-PickerView:3.2.4'
②使用
TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date,View v) {//选中事件回调
SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date(date.getTime());
String time = sm.format(d);
Toast.makeText(getApplicationContext(),time, Toast.LENGTH_LONG).show();
}
}).build();
pvTime.setDate(Calendar.getInstance());//注:根据需求来决定是否使用该方法(一般是精确到秒的情况),此项可以在弹出选择器的时候重新设置当前时间,避免在初始化之后由于时间已经设定,导致选中时间与当前时间不匹配的问题。
pvTime.show();
compile 'com.feezu.liuli:timeselector:1.1.3'
TimeSelector timeSelector = new TimeSelector(this, new TimeSelector.ResultHandler() {
@Override
public void handle(String time) {
Toast.makeText(getApplicationContext(), time, Toast.LENGTH_LONG).show();
}
}, "2015-11-22 17:34", "2016-12-1 15:20");
// 2015-11-22 17:34 时间起点(时间起点不能大于时间终点)
//2016-12-1 15:20 时间终点
timeSelector.show();
TimeSelector timeSelector = new TimeSelector(this, new TimeSelector.ResultHandler() {
@Override
public void handle(String time) {
Toast.makeText(getApplicationContext(), time, Toast.LENGTH_LONG).show();
}
}, "2015-10-30 10:34", "2015-12-1 17:34","9:00","17:00");
timeSelector.show();