如何在CalendarView中禁用将来的日期 [英] How to disable future dates in calendarView

查看:254
本文介绍了如何在CalendarView中禁用将来的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的代码

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


}

}

推荐答案

解决问题的方法很简单。只需将MaxDate设置为当前日期即可。您可以像这样编写代码:

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!

这篇关于如何在CalendarView中禁用将来的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆