gpt4 book ai didi

java - Android:如何禁用 DatePickerFragment 上的 future 日期

转载 作者:行者123 更新时间:2023-12-01 08:59:07 25 4
gpt4 key购买 nike

我在 android 中使用 DatePickerFragment 创建了一个日期选择器。

下面是我的代码,

@OnClick(R.id.add_info_food_date_edit)
public void setDate() {

DatePickerFragment datePickerFragment = new DatePickerFragment();
datePickerFragment.setCallbackListener(new DatePickerFragment.onDatePickerListener() {
@Override
public void onDataSet(int year, int month, int day) {

int currentMonth = 0;

if (month == 0) {
currentMonth = 1;
} else if (month == 1) {
currentMonth = 2;
} else if (month == 2) {
currentMonth = 3;
} else if (month == 3) {
currentMonth = 4;
} else if (month == 4) {
currentMonth = 5;
} else if (month == 5) {
currentMonth = 6;
} else if (month == 6) {
currentMonth = 7;
} else if (month == 7) {
currentMonth = 8;
} else if (month == 8) {
currentMonth = 9;
} else if (month == 9) {
currentMonth = 10;
} else if (month == 10) {
currentMonth = 11;
} else if (month == 11) {
currentMonth = 12;
} else if (month == 12) {

}

dateEditText.setText(day + "/" + currentMonth + "/" + year);
}
});

DialogFragment datePicker = datePickerFragment;

datePicker.show(getSupportFragmentManager(), "datePicker");
}

现在我想禁用此日期选择器中的 future 日期而不破坏此代码。不过我对这个改变还没有什么好的想法。我怎样才能做到这一点。

有什么想法吗?

最佳答案

将MaxDate设置为当前日期,如下所示

datePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

已更新

DialogFragment newFragment = datePickerFragment;
newFragment.show(getFragmentManager(), "datePicker");
getFragmentManager().executePendingTransactions(); // commits the show method from above
DatePickerDialog dialog = (DatePickerDialog) newFragment.getDialog();
DatePicker datePicker = (DatePicker) dialog.getDatePicker();
Date date = new Date();
datePicker.setMaxDate(date.getTime());

关于java - Android:如何禁用 DatePickerFragment 上的 future 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41823355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com