使いたいFragmentなどでこう書く↓
override fun showDatePickerDialog() { DatePickerDialog.newInstance( requestCode = REQUEST_DATE_PICKER, target = this, editText = binding?.pageRegisterBirthdayEditText ).show(parentFragmentManager, DatePickerDialog.TAG) }
DatePickerDialog自体はこう書く
class 生年月日Dialog(editText: RoundEditText?) : DialogFragment(), android.app.DatePickerDialog.OnDateSetListener { val editText = editText companion object { val TAG = android.app.DatePickerDialog::class.java.simpleName @JvmStatic fun newInstance( requestCode: Int, target: Fragment?, editText: RoundEditText? ): DatePickerDialog { val dialog = DatePickerDialog(editText) dialog.setTargetFragment(target, requestCode) return dialog } } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val calendar = Calendar.getInstance() calendar.set(1980, 0, 1) val year = calendar.get(Calendar.YEAR) val month = calendar.get(Calendar.MONTH) val day = calendar.get(Calendar.DAY_OF_MONTH) val dp = android.app.DatePickerDialog( requireContext(), android.R.style.Theme_Holo_Dialog, this, year, month, day ) dp.datePicker.calendarViewShown = false return dp } override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) { val editTextBirthday = editText editTextBirthday?.setText(String.format("%d/%02d/%02d", year, month + 1, dayOfMonth)) } }