在Android中更新CalendarView中的输入日期

时间:2019-06-17 12:13:11

标签: java android android-layout calendarview

我有一个Calendarview和一个Edittext。我希望以DD / MM / YYYY格式输入在编辑文本中输入的日期,并将其设置在Calendarview上。该视图应将当前日期气泡更新为设置的日期。

我已经尝试过: How to set focus on a specific date in CalendarView knowing date is "dd/mm/yyyy"

这是我当前的代码:

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2019);
        calendar.set(Calendar.MONTH, Calendar.JUNE);
        calendar.set(Calendar.DAY_OF_MONTH, 20);

        long milliTime = calendar.getTimeInMillis();
        CalendarView calendarView =  findViewById(R.id.calendar);
        calendarView.setFocusedMonthDateColor(Color.BLUE); //apparently this is deprecated so won't work. Not working without it either.
        calendarView.setDate (milliTime); //this is supposed to change the date but isn't

谢谢!

1 个答案:

答案 0 :(得分:-1)

我已阅读您的问题,并已尝试解决此问题。像这样calendar_view.setDate(milliTime);那样在long型值中传递日期(以毫秒为单位的时间)以在日历视图中设置日期,请尝试使用此代码来实现您的目标。

代码

    calendar_view = findViewById(R.id.calendar_view);

  //replace date variable static value to your edit text 
    value    
  String date = "25/06/2019";
    String parts[] = date.split("/");

    int day = Integer.parseInt(parts[0]);
    int month = Integer.parseInt(parts[1]);
    int year = Integer.parseInt(parts[2]);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.DAY_OF_MONTH, day);

    long milliTime = calendar.getTimeInMillis();
    calendar_view.setDate(milliTime);