简体   繁体   English

CalendarView总是返回当前日期,而忽略用户选择了什么

[英]CalendarView returns always current day ignoring what user selects

There is a CalendarView in screen and I want to display the selected date on the same screen. 屏幕上有一个CalendarView,我想在同一屏幕上显示选定的日期。 I want it to change dynamically as user changes the selected date. 我希望它随着用户更改所选日期而动态更改。

This works perfect in my phone LG G2. 这在我的手机LG G2中非常完美。 Whenever I select a date in calendar, it shows that date. 每当我在日历中选择一个日期时,它就会显示该日期。 But in Nexus 10 tablet whatever date I select, it returns the current day. 但是在Nexus 10平板电脑中,无论我选择什么日期,它都会返回当天。 And if I keep changing the date, nothing changes it always display the current day. 如果我不断更改日期,则不会更改任何内容,它始终显示当前日期。

Here is the relevant part of my code. 这是我的代码的相关部分。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootview = inflater.inflate(R.layout.calendar_layout, container, false);
    calendarView = (CalendarView) rootview.findViewById(R.id.calendarView);
    selectedDate = (TextView) rootview.findViewById(R.id.textView5);
    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener(){
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth){
            date = new Date(calendarView.getDate());
            dateString = simpleDate.format(date);
            MainActivity.selectedDate=dateString;
            selectedDate.setText(dateString);
        }
    });
    return rootview;
}

Is there anything wrong with the code? 代码有什么问题吗? What may be reason that CalendarView acts different in phone and tablet? CalendarView在手机和平​​板电脑上的行为不同可能是什么原因? Thank you for your answers. 谢谢您的回答。

I had the exact same problem on devices with Lollipop. 我在使用棒棒糖的设备上遇到了完全相同的问题。 The only thing I could think of was working with a Calendar within the onSelectedDayChange 我唯一想到的就是在onSelectedDayChange使用Calendar

public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth){
    Calendar c = Calendar.getInstance();
    c.set(year, month, dayOfMonth);
    Date d = c.getTime; // here is your correct date
}

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

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