栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android日期和时间选择器实现代码

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android日期和时间选择器实现代码

抽出来了一个方法来选择时间(这里自己规定的只能选择当前时间以后的日期),日期选择完毕就会自动弹出时间选择器让选择时间。

  
  private void selectDataAndTime() {
    // 获取当前时间
    final Calendar calendar = Calendar.getInstance();
    
    // 日期选择对话框
    dataPickerDialog = new DatePickerDialog(this, new onDateSetListener() {

      @Override
      public void onDateSet(DatePicker view, int year, int month, int day) {
 // 判断用户选择的日期是否合法
 if (calendar.get(Calendar.YEAR) > year) {
   toast("时间有误,请从新选择");
   return;
 } else if (calendar.get(Calendar.YEAR) == year) {
   if (calendar.get(Calendar.MONTH) > month) {
     toast("时间有误,请从新选择");
     return;
   } else if (calendar.get(Calendar.MONTH) == month) {
     if (calendar.get(Calendar.DAY_OF_MONTH) > day) {
toast("时间有误,请从新选择");
return;
     } else {
strDate = year + "-" + (month + 1) + "-" + day;
if (timePickerDialog != null) {
  timePickerDialog.show();
}
     }
   } else {
     strDate = year + "-" + (month + 1) + "-" + day;
     if (timePickerDialog != null) {
timePickerDialog.show();
     }
   }
 } else {
   strDate = year + "-" + (month + 1) + "-" + day;
   if (timePickerDialog != null) {
     timePickerDialog.show();
   }
 }
      }
    }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar
 .get(Calendar.DAY_OF_MONTH));
    // 时间选择对话框
    timePickerDialog = new TimePickerDialog(this, new onTimeSetListener() {

      @Override
      public void onTimeSet(TimePicker view, int hour, int minute) {
 strTime = strDate + " " + hour + ":" + minute;
 timeTt.setText(strTime);
      }
    }, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), true);
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/157295.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号