今天尝试自己动手做一个日期选择器
1、在布局文件中添加DatePicker
... <DatePicker android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/datepicker" > </DatePicker> ...
2、java代码:
通过id获取布局里的datepicker:
datePicker=findViewById(R.id.datepicker);
声明一个日历对象Calendar获取当前时间的日历(不声明Calendar则datepicker默认1900年)
Calendar calendar=Calendar.getInstance();//获取当前时间的日历,不然默认1900年
年月日赋值:
year=calendar.get(Calendar.YEAR);//今年 month=calendar.get(Calendar.MONTH);//今月 day=calendar.get(Calendar.DAY_OF_MONTH)