如何在calendarView中禁用将来的日期[复制]

问题描述 投票:1回答:2

这个问题在这里已有答案:

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

这是我的代码

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


}

}

android date calendarview
2个回答
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);

我希望这有帮助..快乐编码!


0
投票

你试过了吗:

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

calendarKegiatan.setMaxDate(System.currentTimeMillis());

这应该禁用所有未来日期。

© www.soinside.com 2019 - 2024. All rights reserved.