CalendarView today date item click

前端 未结 8 1168
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 10:49

I searched a lot on the internet and I couldn\'t succeed to find correct solution for CalendarView click on today date.

8条回答
  •  余生分开走
    2021-01-18 11:18

    Try this with below way:

    //First set current day,month,year :
    
    Calendar c = Calendar.getInstance();
    int prevDay = c.get(Calendar.DAY_OF_MONTH);
    int prevMonth = c.get(Calendar.MONTH);
    int prevYear = c.get(Calendar.YEAR);
    Date d = new Date(prevYear, prevMonth, prevDay);
    c.setTime(d);
    

    Then use dateChangeListener :

    CalendarView calendar = (CalendarView) findViewById(R.id.calendarViewCN);
    calendar.setOnDateChangeListener(new OnDateChangeListener() {
    @Override
    public void onSelectedDayChange(CalendarView view, int year,int month, int dayOfMonth) {
    boolean changed = changeUpdate(year, month, dayOfMonth);
    if (changed) {
    Toast.makeText(getApplicationContext(),
    "New Y: " + year + " M: " + month + " D: "+dayOfMonth, Toast.LENGTH_LONG).show();
    }
    }
    });
    

    Now declare the changeUpdate() :

    private boolean changeUpdate(int curYear, int curMonth, int curDay) {
    boolean changed = false;
    if (curDay != prevDay || curMonth!=prevMonth || curYear!=prevYear) {
    changed = true;
    }
    

提交回复
热议问题