[TIL] #33 Date Picker Dialog

Yeon·2023년 9월 6일
0

내일배움캠프 - Kotlin

목록 보기
44/58
post-thumbnail

1. Xml Code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/ice_blue"
    android:padding="16dp">

    <DatePicker
        android:id="@+id/dialog_datepicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:calendarViewShown="false"
        android:datePickerMode="spinner"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/dialog_datepicker_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/pale_gray"
        android:text="cancel"
        android:textColor="@color/charcoal_black"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/dialog_datepicker" />

    <Button
        android:id="@+id/dialog_datepicker_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/pale_gray"
        android:text="Save"
        android:textColor="@color/charcoal_black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/dialog_datepicker" />
</androidx.constraintlayout.widget.ConstraintLayout>

2. Kotlin Code

class DatePickDialog : BottomSheetDialogFragment() {
    private var _binding: DialogDatePickBinding? = null
    private val binding get() = _binding!!

    private var dateSelectedListener: DateSelectedListener? = null

    companion object {
        const val TAG = "DatePickDialog"
        private const val ARG_TEXT = "text"

        fun newInstance(text: String): DatePickDialog {
            val fragment = DatePickDialog()
            val args = Bundle()
            args.putString(ARG_TEXT, text)
            fragment.arguments = args
            return fragment
        }
    }

    interface DateSelectedListener {
        fun onDateSelected(formattedDate: String)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = DialogDatePickBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
    }

    private fun initViews() = with(binding) {
        val datePicker = dialogDatepicker
        val saveButton = dialogDatepickerSave
        val cancelButton = dialogDatepickerCancel

        val dateFormat = "%04d-%02d-%02d"

        val calendar = Calendar.getInstance()
        var selectedYear = calendar.get(Calendar.YEAR)
        var selectedMonth = calendar.get(Calendar.MONTH)
        var selectedDay = calendar.get(Calendar.DAY_OF_MONTH)

        // DatePicker의 최대 날짜 설정
        val maxDateCalendar = Calendar.getInstance()
        maxDateCalendar.set(selectedYear, selectedMonth, selectedDay, 0, 0, 0)
        datePicker.maxDate = maxDateCalendar.timeInMillis

        // DatePicker의 날짜 변경 이벤트 처리
        datePicker.init(
            selectedYear,
            selectedMonth,
            selectedDay
        ) { _, year, monthOfYear, dayOfMonth ->
            selectedYear = year
            selectedMonth = monthOfYear + 1
            selectedDay = dayOfMonth
            val formattedDate = String.format(
                dateFormat,
                year,
                monthOfYear + 1,
                dayOfMonth
            )
            dateSelectedListener?.onDateSelected(formattedDate)
        }

        saveButton.setOnClickListener {
            val formattedDate = String.format(
                dateFormat,
                selectedYear,
                selectedMonth,
                selectedDay
            )
            dateSelectedListener?.onDateSelected(formattedDate)
            dismiss()
        }

        cancelButton.setOnClickListener {
            dismiss()
        }
    }

    fun setDateSelectedListener(listener: DateSelectedListener) {
        this.dateSelectedListener = listener
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

0개의 댓글