如何禁用日期选择器中的上一个日期?
how to disable preveious date in a date picker?
我正在使用公历,我需要在 android 中禁用前一个日期到当前日期。我已经检查过 setMinDate 方法,但它不起作用.. 谁能帮我?这是我的代码..
public void SelectDateTime() {
final View dialogView = View.inflate(getApplicationContext(), R.layout.date_picker_activate, null);
DateTimeDialog = new AlertDialog.Builder(getApplicationContext()).create();
dialogView.findViewById(R.id.cancelBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DateTimeDialog.dismiss();
}
});
dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.datePicker1);
TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.timePicker);
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
datePicker.getMinDate();
final long today1 = System.currentTimeMillis() - 1000;
calendar.after(System.currentTimeMillis());
// datePicker.setMinDate(calendar.getTimeInMillis());
使用此代码
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
// dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
dialog.getDatePicker().setMinDate(c.getTimeInMillis());
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
txt_date.setText(String.format(Locale.ENGLISH, "%02d/%02d/%d", day, month + 1, year));
}
}
像
一样在侧边按钮点击中调用此方法
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
This line set min date - dialog.getDatePicker().setMinDate(c.getTimeInMillis());
并使用它来设置最大日期
dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
这将禁用以前的日期
public void showDateToSelect() {
// Get Current Date
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
/*DatePicker dialog to select date*/
DatePickerDialog datePickerDialog = new DatePickerDialog(mContext,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
textView.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
/*To show only dates from today to future*/
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
}
使用此库
implementation 'com.github.florent37:singledateandtimepicker:1.2.1'
并使用以下代码启动
startSingleDateAndTimePickerDialog = new SingleDateAndTimePickerDialog.Builder(context);
startSingleDateAndTimePickerDialog
.bottomSheet()
.curved()
.displayHours(true)
.displayMinutes(true)
.minDateRange(new Date(minDate))
.defaultDate(new Date(defaultDate))
.mustBeOnFuture()
.displayListener(new SingleDateAndTimePickerDialog.DisplayListener() {
@Override
public void onDisplayed(SingleDateAndTimePicker picker) {
//retrieve the SingleDateAndTimePicker
}
})
.title("Date & Time")
.listener(new SingleDateAndTimePickerDialog.Listener() {
@Override
public void onDateSelected(Date date) {
Date currentDate = new Date(System.currentTimeMillis());
String dateTime = getDate(date.getTime() / 1000);
textview.settext(dateTime);
}
}).display();
public static String getDate(long timeStamp) {
Date time = new Date(timeStamp*1000);
SimpleDateFormat df2 = new SimpleDateFormat("MMM dd,yyy h:mm a");
return df2.format(time);
}
我正在使用公历,我需要在 android 中禁用前一个日期到当前日期。我已经检查过 setMinDate 方法,但它不起作用.. 谁能帮我?这是我的代码..
public void SelectDateTime() {
final View dialogView = View.inflate(getApplicationContext(), R.layout.date_picker_activate, null);
DateTimeDialog = new AlertDialog.Builder(getApplicationContext()).create();
dialogView.findViewById(R.id.cancelBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DateTimeDialog.dismiss();
}
});
dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.datePicker1);
TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.timePicker);
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
datePicker.getMinDate();
final long today1 = System.currentTimeMillis() - 1000;
calendar.after(System.currentTimeMillis());
// datePicker.setMinDate(calendar.getTimeInMillis());
使用此代码
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
// dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
dialog.getDatePicker().setMinDate(c.getTimeInMillis());
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
txt_date.setText(String.format(Locale.ENGLISH, "%02d/%02d/%d", day, month + 1, year));
}
}
像
一样在侧边按钮点击中调用此方法DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
This line set min date - dialog.getDatePicker().setMinDate(c.getTimeInMillis());
并使用它来设置最大日期
dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
这将禁用以前的日期
public void showDateToSelect() {
// Get Current Date
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
/*DatePicker dialog to select date*/
DatePickerDialog datePickerDialog = new DatePickerDialog(mContext,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
textView.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
/*To show only dates from today to future*/
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
}
使用此库
implementation 'com.github.florent37:singledateandtimepicker:1.2.1'
并使用以下代码启动
startSingleDateAndTimePickerDialog = new SingleDateAndTimePickerDialog.Builder(context);
startSingleDateAndTimePickerDialog
.bottomSheet()
.curved()
.displayHours(true)
.displayMinutes(true)
.minDateRange(new Date(minDate))
.defaultDate(new Date(defaultDate))
.mustBeOnFuture()
.displayListener(new SingleDateAndTimePickerDialog.DisplayListener() {
@Override
public void onDisplayed(SingleDateAndTimePicker picker) {
//retrieve the SingleDateAndTimePicker
}
})
.title("Date & Time")
.listener(new SingleDateAndTimePickerDialog.Listener() {
@Override
public void onDateSelected(Date date) {
Date currentDate = new Date(System.currentTimeMillis());
String dateTime = getDate(date.getTime() / 1000);
textview.settext(dateTime);
}
}).display();
public static String getDate(long timeStamp) {
Date time = new Date(timeStamp*1000);
SimpleDateFormat df2 = new SimpleDateFormat("MMM dd,yyy h:mm a");
return df2.format(time);
}