如何打开DatePicker从当前日期到18年前的日期?

问题描述 投票:0回答:3

我想打开从当前日期到 18 年前的日期选择器以进行出生日期验证。

android validation datepicker date
3个回答
3
投票

只需添加以下行

 myCalendar.add(Calendar.YEAR,-18)
 new DatePickerDialog(getActivity(), dateExpiry, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }

1
投票

这可能不正是您一直在寻找的东西。但无论如何,你可以有一个起点并操纵它。请确保更新下面提到的答案。

在 XML 文件中尝试一下:

<EditText
   android:id="@+id/Birthday"
   custom:font="@string/font_avenir_book"
   android:clickable="true"
   android:editable="false"
   android:hint="@string/birthday"/>

现在在 Java 文件中:

final Calendar myCalendar = Calendar.getInstance();

EditText edittext= (EditText) findViewById(R.id.Birthday);
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    }

};

edittext.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        new DatePickerDialog(classname.this, date, myCalendar
                .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)).show();
    }
});

现在在上面的活动中添加方法。

private void updateLabel() {
    String myFormat = "MM/dd/yy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    edittext.setText(sdf.format(myCalendar.getTime()));
}

在 EditText 的 xml 文件中添加

android:focusable="false"
以允许单点触摸。

这个答案是从Datepicker:如何在单击edittext时弹出日期选择器复制的。


0
投票

问题已得到解答,但他错过了允许的最大数据。 下面的线为您提供最小范围和最大范围

 private fun showDatePicker() {
    val eighteenYearBefore = Calendar.getInstance()
    eighteenYearBefore.add(Calendar.YEAR, -18) // Max range for dob
    eighteenYearBefore.add(Calendar.DATE, -1)
    //val calendar = Calendar.getInstance()
    val datePickerDialog = DatePickerDialog(
        requireActivity(), { _, year: Int, monthOfYear: Int, dayOfMonth: Int ->
            val selectedDate = Calendar.getInstance()
            selectedDate.set(year, monthOfYear, dayOfMonth)
            val formattedDate = selectedDate.time.getFormattedDate(AppConstants.DATE_FORMAT_DOB)
            binding.etDob.setText(formattedDate)
        },
        eighteenYearBefore.get(Calendar.YEAR),
        eighteenYearBefore.get(Calendar.MONTH),
        eighteenYearBefore.get(Calendar.DAY_OF_MONTH)
    )

    val hundredYearBack = Calendar.getInstance()
    hundredYearBack.add(Calendar.YEAR, -100) // Min range for DOB you can change as per requirment
    hundredYearBack.add(Calendar.DATE, -1)
    
    datePickerDialog.datePicker.maxDate =  eighteenYearBefore.time.time
    datePickerDialog.datePicker.minDate =  hundredYearBack.time.time
    datePickerDialog.show()
}
© www.soinside.com 2019 - 2024. All rights reserved.