【问题标题】:select date of birth dialog选择出生日期对话框
【发布时间】:2015-09-07 21:05:48
【问题描述】:

如何创建可以选择出生日期的警报对话框:

我希望将来选择日期不是不可能的。

如何在日期对话框中设置限制 这是我的代码

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                final Calendar calendar = Calendar.getInstance();
                int yy = calendar.get(Calendar.YEAR);
                int mm = calendar.get(Calendar.MONTH);
                int dd = calendar.get(Calendar.DATE);
                return new DatePickerDialog(this, mDatePickerListener, yy, mm, dd);
        }
        return null;
    }

【问题讨论】:

    标签: android android-dialogfragment android-datepicker android-date


    【解决方案1】:

    添加这个

    dialog.getDatePicker().setMaxDate(new Date().getTime());
    

    【讨论】:

      【解决方案2】:

      我建议使用一个非常酷的 Material Design 日期选择器 library,它向后兼容。

      您需要做的就是将以下行添加到您的 gradle 依赖项列表中:

      compile 'com.wdullaer:materialdatetimepicker:1.5.2' // The latest at this point.
      

      然后您只需使用 DatePickerDialog 的 setMinDate() 和 setYearRange() 方法。

      类似这样的:

      Calendar calendar = Calendar.getInstance();
      DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
              @Override
              public void onDateSet(DatePickerDialog datePickerDialog, int i, int i1, int i2) {
                  // Do whatever you want when the date is selected.
              }
      }, calendar.get(Calendar.YEAR), 
         calendar.get(Calendar.MONTH),
         calendar.get(Calendar.DAY_OF_MONTH));
         datePickerDialog.setMinDate(calendar);
         datePickerDialog.setYearRange(calendar.get(Calendar.YEAR), 
                     calendar.get(Calendar.YEAR) + YEARS_IN_THE_FUTURE); // You can add your value for YEARS_IN_THE_FUTURE.
      

      这有帮助吗?

      【讨论】:

      • 为我提供帮助。只需添加它以在片段中显示它:datePickerDialog.show(getActivity().getFragmentManager(), "DatePickerDialog");
      【解决方案3】:

      如果您在代码中使用kotlin,那么这将对您有所帮助

      注意::textView 保存要设置日期的文本的引用

      private fun openDobPicker(textView: TextView) {
              val c = Calendar.getInstance()
              val year = c.get(Calendar.YEAR)
              val month = c.get(Calendar.MONTH)
              val day = c.get(Calendar.DAY_OF_MONTH)
      
              val dpd = DatePickerDialog(requireActivity(), DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
                  val date = "${dayOfMonth}/${monthOfYear}/${year}"
                  textView.text = date
              }, year, month, day)
      
              //restrict calendar to show future dates
              dpd.datePicker.maxDate = Date().time
              
              //If you want to limit the minimum date then do this
              //dpd.datePicker.minDate = PASSED_MIN_DATE_HERE
          
      
              
              dpd.show()
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-07
        • 2018-09-28
        • 1970-01-01
        • 1970-01-01
        • 2014-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多