如何在CalendarView中禁用将来的日期

时间:2019-02-23 02:31:20

标签: android date calendarview

我想禁用第二天而不是前一天。我已经找到了第二天如何禁用它的方法,但是在前一天也禁用了它。请帮助我

这是我的代码

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);


}

}

2 个答案:

答案 0 :(得分:0)

您尝试过吗:

calendarKegiatan = (CalendarView) findViewById(R.id.calendarKegiatan);

calendarKegiatan.setMaxDate(System.currentTimeMillis());

这应该禁用所有将来的日期。

答案 1 :(得分:0)

解决问题的方法非常简单。只需将MaxDate设置为当前日期。您可以使用如下代码:

//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);

我希望这会有所帮助。.编码愉快!