如何在安卓工作室的MyCalender DialogFragment中禁用未来日期



这是我在MyCalendar.java中的代码。我想在我的android应用程序中禁用未来的日期。我是安卓工作室的初学者,所以我不熟悉它的大部分功能。有人能帮我处理这个代码吗?

package com.example.attendenceapp;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.DatePicker;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.DialogFragment;
import java.util.Calendar;
public class MyCalendar extends DialogFragment{
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
public interface OnCalendarOkClickListener{
void onClick(int year, int month, int day);
}
public OnCalendarOkClickListener onCalendarOkClickListener;
public void setOnCalendarOkClickListener(MyCalendar.OnCalendarOkClickListener onCalendarOkClickListener) {
this.onCalendarOkClickListener = onCalendarOkClickListener;
}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(),((view,year,month,dayOfMonth)-> {
onCalendarOkClickListener.onClick(year,month,dayOfMonth);
}),calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));

}

void setDate(int year,int month,int day){
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,day);
}
String getDate(){
return DateFormat.format("dd.MM.yyyy",calendar).toString();
}
}

你可以试试这个:

public void openDatePickerDialog() {
// Get Current Date
Calendar cal = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(EditPostActivity.this,
(view, year, monthOfYear, dayOfMonth) -> {
String selectedDate = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
binding.etDate.setText(selectedDate);
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));

datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
datePickerDialog.show();
}

相关内容

  • 没有找到相关文章