正文  电脑教程 > 服务器教程 >

Android日期选择器实例代码详解

废话不多说了,直接给大家贴代码了,具体代码如下所示: //出生年月设置 private void birthSetting() { Calend...

废话不多说了,直接给大家贴代码了,具体代码如下所示:

//出生年月设置
private void birthSetting() {
  Calendar calendar = Calendar.getInstance();
  calendar.add(Calendar.DAY_OF_YEAR, 1);
  new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
      String month = (monthOfYear + 1) < 10 ? "0" + String.valueOf(monthOfYear + 1) : String.valueOf(monthOfYear + 1);
      tvBirth.setText(new StringBuilder().append(year).append("-").append(month).append("-").append(dayOfMonth));//tvBirth就是选择日期结果textview
    }
  }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
      calendar.get(Calendar.DAY_OF_MONTH)).show();
}