本文主要是介绍TimePickerDialog和DatePickerDialog使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
TimePickerDialog
TimePickerDialog(Context context, TimePickerDialog.OnTimeSetListener listener, int hourOfDay, int minute, boolean is24HourView)
参数
context:当前上下文
listener:时间改变监听器
hourOfDay:初始化的小时
minute:初始化的分钟
is24HourView:是否以24小时显示时间
关键代码:
new TimePickerDialog(this,new TimePickerDialog.OnTimeSetListener() {@Overridepublic void onTimeSet(TimePicker view, int hourOfDay, int minute) {textView.setText(hourOfDay+":"+minute);}}, 0, 0, true).show();
DatePickerDialog
用法和TimePickerDialog相同
首先调用Calendar类获取年月日,然后将获取到的年月日放进new出来的DatePickerDialog中,这样就可以默认选中当前日期
private void chooseDate(){Calendar ca = Calendar.getInstance();mYear = ca.get(Calendar.YEAR);mMonth = ca.get(Calendar.MONTH);mDay = ca.get(Calendar.DAY_OF_MONTH);DatePickerDialog datePickerDialog = new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener() {@Overridepublic void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {final String data = year+"年"+(month+1) + "月" + dayOfMonth + "日 ";textView.setText(data);}},mYear, mMonth, mDay);datePickerDialog.show();}
这篇关于TimePickerDialog和DatePickerDialog使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!