我想选择过去的日期到当前的最大日期。在日期选择器的单个日期选择中有诸如setMinDate()和setMaxDate()之类的方法。但是,如何在重要日期范围选择器中的日期范围选择中做到这一点?
答案 0 :(得分:2)
您必须使用 CalendarConstraints 来限制日期范围选择器中的最小和最大日期。
val constraintsBuilderRange = CalendarConstraints.Builder()
val dateValidatorMin: DateValidator = DateValidatorPointForward.from(millisOfMinimum)
val dateValidatorMax: DateValidator = DateValidatorPointBackward.before(millisOfMaximum)
val listValidators = ArrayList<DateValidator>()
listValidators.add(dateValidatorMin)
listValidators.add(dateValidatorMax)
val validators = CompositeDateValidator.allOf(listValidators)
constraintsBuilderRange.setValidator(validators)
val datePicker = MaterialDatePicker.Builder.dateRangePicker()
.setTitleText("Select range")
.setCalendarConstraints(constraintsBuilderRange.build())
.build()
答案 1 :(得分:1)
在 setMaxDate()
中设置当前日期和 setMinDate()想要显示的任何内容。
答案 2 :(得分:0)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showRangePickerDialog()
}
private fun showRangePickerDialog() {
val builderRange = MaterialDatePicker.Builder.dateRangePicker()
builderRange.setCalendarConstraints(defineRange().build())
val pickerRange = builderRange.build()
pickerRange.show(supportFragmentManager, pickerRange.toString())
}
private fun defineRange(): CalendarConstraints.Builder {
val constraintsBuilderRange = CalendarConstraints.Builder()
val calendarStart: Calendar = GregorianCalendar.getInstance()
val calendarEnd: Calendar = GregorianCalendar.getInstance()
val year = 2020
calendarStart.set(year, 1, 1)
calendarEnd.set(year, 6, 30)
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) {
}
override fun describeContents(): Int {
}
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)
}
}
}
尝试使用上面的代码,但必须根据需要对范围进行一些更改。
答案 3 :(得分:0)
只需在您的代码中添加这两行代码,您就可以防止将来选择日期。
maxDate:moment().subtract(0, 'days'), “本月”:[moment().startOf("month"), moment().subtract(0, 'days')],
var rangesVal = {
position: "inherit",
drops: "down",
maxDate: moment().subtract(0, 'days'),
minDate: minDate,
opens: "center",
maxSpan: {
months: 12,
},
ranges: {
Today: [moment(), moment()],
Yesterday: [moment().subtract(1, "days"), moment().subtract(1, "days")],
"Last 7 Days": [moment().subtract(6, "days"), moment()],
"Last 30 Days": [moment().subtract(29, "days"), moment()],
"This Month": [moment().startOf("month"), moment().subtract(0, 'days')],
"Last Month": [
moment().subtract(1, "month").startOf("month"),
moment().subtract(1, "month").endOf("month"),
],
},
};