问题
I want to disable the next day but not the previous day. I have found out how to disable it for the next day, but for the previous day it was also disabled. Please help me
This is my code
public class Kegiatan extends AppCompatActivity {
private CalendarView calendarKegiatan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kegiatan);
calendarKegiatan = (CalendarView) findViewById(R.id.calendarKegiatan);
calendarKegiatan.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int day) {
Date getdate = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/M/d");
String dateNow = dateFormat.format(getdate);
String date = year + "/" + (month + 1) + "/" + day;
if (date.equals(dateNow)) {
Intent intent = new Intent(Kegiatan.this, ListKegiatan.class);
intent.putExtra("tanggal", date);
startActivity(intent);
} else {
Toast.makeText(Kegiatan.this, "Dilarang laporan di hari selanjutnya", Toast.LENGTH_SHORT).show();
}
}
});
calendarKegiatan.setFocusedMonthDateColor(Color.GREEN);
}
}
回答1:
The solution to your problem is quite simple. Simply setMaxDate to the current date. You can have your code like this:
//Declare and initialize your calendarView object.
calendarKegiatan = (CalendarView) findViewById(R.id.calendarKegiatan);
//Set the maximum date
calendarKegiatan.getDatePicker().setMaxDate(System.currentTimeMillis());
//Listen for changes to the date and paste your original logic
calendarKegiatan.setOnDateChangeListener(new CalendarView.OnDateChangeListener(){...};
//Do more awesome stuff
calendarKegiatan.setFocusedMonthDateColor(Color.GREEN);
I hope this helps.. Merry coding!
回答2:
Did you try:
calendarKegiatan = (CalendarView) findViewById(R.id.calendarKegiatan);
calendarKegiatan.setMaxDate(System.currentTimeMillis());
This should disable all future dates.
来源:https://stackoverflow.com/questions/54837654/how-to-disable-future-dates-in-calendarview