import android.app.DatePickerDialog import android.app.Dialog import android.os.Bundle import android.widget.DatePicker import androidx.fragment.app.DialogFragment import com.greenhcm.android.common.function.toDateFormat import com.greenhcm.android.common.function.toTimeMilis import java.util.* class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener { private lateinit var action : (String) -> Unit private var maxDate : String? = null private var minDate : String? = null override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { // Use the current date as the default date in the picker val c = Calendar.getInstance() val year = c.get(Calendar.YEAR) val month = c.get(Calendar.MONTH) val day = c.get(Calendar.DAY_OF_MONTH) // Create a new instance of DatePickerDialog and return it return DatePickerDialog(requireContext(), this, year, month, day).apply { maxDate?.takeIf { it.isNotEmpty() }?.let { datePicker.maxDate = it.toTimeMilis("yyyy-MM-dd") } minDate?.takeIf { it.isNotEmpty() }?.let { datePicker.minDate = it.toTimeMilis("yyyy-MM-dd") } } } override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) { action("$year-${month+1}-$day") } fun setListener( maxDate: String? = null, minDate: String? = null, action: (String) -> Unit ) { this.action = action this.maxDate = maxDate this.minDate = minDate } }
Hello guys i am beginner android developer, i have problem to limit date in date picker dialog, above my fragment date picker , code below call from above fragment but i can not limit date picker , i search other reference but not help, need help guys, thank you.how to limit date in set listener below? i want to limit date, start today and disabled yesterday.
etDate.setOnClickListener { val newFragment = DatePickerFragment().apply { setListener { setDate(it) } } newFragment.show(childFragmentManager, "timePicker") }
https://stackoverflow.com/questions/67295217/datepicker-minimum-date-kotlin-seriously-problem April 28, 2021 at 02:56PM
没有评论:
发表评论