DatePickerDialog
其实就是把CalendarView
封装进Dialog
中了,调用起来更方便快捷。
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, listener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
});
构造方法也很简单
然后调用show
方法即可。
也可以在上下文参数后面加上自定义的dialog style,默认获取当前activity的theme
。
private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
Log.i("---", "年=" + year + ",月=" + (monthOfYear + 1) + ",日=" + dayOfMonth);
tvInfo.setText(year+"年"+(monthOfYear + 1)+"月"+dayOfMonth+"日");
}
};
【注意】月份下标是从0
开始的,所以+1
。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有