Restrict Range in Android DatePicker Custom Dialog

后端 未结 3 1447
野的像风
野的像风 2020-12-21 03:08

I have a custom linear layout which has DatePicker and TimePicker widgets in it. This is used as DateTime picker. I want to restrict t

相关标签:
3条回答
  • 2020-12-21 03:24

    The Material Components Library provides the MaterialDatePicker.
    You can use a DateValidator to restrict the selections.
    In particular you can use the built-in validators:

    • DateValidatorPointForward that enables dates from a given point forward
    • DateValidatorPointBackward that enables only dates before a given point.

    Something like:

    MaterialDatePicker.Builder<Pair<Long, Long>> builderRange = MaterialDatePicker.Builder.dateRangePicker();
    CalendarConstraints.Builder constraintsBuilderRange = new CalendarConstraints.Builder();
    
    //....define min and max for example with LocalDateTime and ZonedDateTime or Calendar
    
    CalendarConstraints.DateValidator dateValidatorMin = DateValidatorPointForward.from(min.getTimeInMillis());
    CalendarConstraints.DateValidator dateValidatorMax = DateValidatorPointBackward.before(max.getTimeInMillis());
    
    ArrayList<CalendarConstraints.DateValidator> listValidators =
                new ArrayList<CalendarConstraints.DateValidator>();
    listValidators.add(dateValidatorMin);
    listValidators.add(dateValidatorMax);
    CalendarConstraints.DateValidator validators = CompositeDateValidator.allOf(listValidators);
    constraintsBuilderRange.setValidator(validators);
    
    builderRange.setCalendarConstraints(constraintsBuilderRange.build());
    MaterialDatePicker<Pair<Long, Long>> pickerRange = builderRange.build();
    pickerRange.show(getSupportFragmentManager(), pickerRange.toString());
    

    0 讨论(0)
  • 2020-12-21 03:30

    For future readers!

    Actually with new android material design components what you want achieve could be achieved using MaterialDatePicker. And dates outside the allowed range is not selectable.


    Steps

    1. Add material dependency to your module's gradle file

    implementation 'com.google.android.material:material:1.1.0-beta01'
    

    2. Change app theme to inherit from a version of material theme.

    ex:

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    

    3. Use following code to initiate the dialog.

    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            setupRangePickerDialog()
    
        }
    
        private fun setupRangePickerDialog() {
            val builderRange = MaterialDatePicker.Builder.dateRangePicker()
    
            builderRange.setCalendarConstraints(limitRange().build())
            val pickerRange = builderRange.build()
            pickerRange.show(supportFragmentManager, pickerRange.toString())
        }
    
    
        /*
        Limit selectable range to Oct 17 - Nov 20 2019
         */
        private fun limitRange(): CalendarConstraints.Builder {
    
            val constraintsBuilderRange = CalendarConstraints.Builder()
    
            val calendarStart: Calendar = GregorianCalendar.getInstance()
            val calendarEnd: Calendar = GregorianCalendar.getInstance()
    
            val year = 2019
    
            calendarStart.set(year, 10, 17)
            calendarEnd.set(year, 11, 20)
    
            val minDate = calendarStart.timeInMillis
            val maxDate = calendarEnd.timeInMillis
    
            constraintsBuilderRange.setStart(minDate)
            constraintsBuilderRange.setEnd(maxDate)
    
            constraintsBuilderRange.setValidator(RangeValidator(minDate, maxDate))
    
            return constraintsBuilderRange
        }
    
    
    }
    
    class RangeValidator(private val minDate:Long, private val maxDate:Long) : CalendarConstraints.DateValidator{
    
    
        constructor(parcel: Parcel) : this(
            parcel.readLong(),
            parcel.readLong()
        )
    
        override fun writeToParcel(dest: Parcel?, flags: Int) {
            TODO("not implemented")
        }
    
        override fun describeContents(): Int {
            TODO("not implemented")
        }
    
        override fun isValid(date: Long): Boolean {
            return !(minDate > date || maxDate < date)
    
        }
    
        companion object CREATOR : Parcelable.Creator<RangeValidator> {
            override fun createFromParcel(parcel: Parcel): RangeValidator {
                return RangeValidator(parcel)
            }
    
            override fun newArray(size: Int): Array<RangeValidator?> {
                return arrayOfNulls(size)
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-21 03:36

    If somebody wants RangeValidator (posted above) but with less code: (@Parcelize annotation added)

    @Parcelize
    internal class RangeValidator(
    private var minDate: Long = 0,
    private var maxDate: Long = 0) : DateValidator {
    
         override fun isValid(date: Long): Boolean {
             return !(minDate > date || maxDate < date)
         }
    }
    
    0 讨论(0)
提交回复
热议问题