简体   繁体   English

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

[英]Update entered date in CalendarView in Android

I have a Calendarview and an Edittext. 我有一个Calendarview和一个Edittext。 I wish to take the date entered in the edittext in the format DD/MM/YYYY and set it on the Calendarview. 我希望以DD / MM / YYYY格式输入在编辑文本中输入的日期,并将其设置在Calendarview上。 The view should update the current date bubble to the set date. 视图应将当前日期气泡更新为设置的日期。

I have already tried: How to set focus on a specific date in CalendarView knowing date is "dd/mm/yyyy" 我已经尝试过: 如何在CalendarView的特定日期上设置焦点,知道日期是“ dd / mm / yyyy”

This is my current code: 这是我当前的代码:

        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

Thanks in advance! 提前致谢!

I have read your question and i have try to solve this problem. 我已阅读您的问题,并已尝试解决此问题。 pass date(time in milliseconds) in long type value to set date in calendar view like this calendar_view.setDate(milliTime); 以长类型值传递日期(以毫秒为单位的时间)以在日历视图中设置日期,例如calendar_view.setDate(milliTime); try this code to achieve your goal . 尝试使用此代码来实现您的目标。

Code

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM