【问题标题】:CalendarView returns always current day ignoring what user selectsCalendarView 总是返回当前日期,而忽略用户选择的内容
【发布时间】:2015-10-14 16:08:58
【问题描述】:

屏幕中有一个 CalendarView,我想在同一屏幕上显示选定的日期。我希望它随着用户更改所选日期而动态更改。

这在我的手机 LG G2 中完美运行。每当我在日历中选择一个日期时,它都会显示该日期。但在 Nexus 10 平板电脑中,无论我选择什么日期,它都会返回当天。如果我不断更改日期,则不会发生任何变化,它始终显示当前日期。

这是我的代码的相关部分。

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

代码有什么问题吗? CalendarView 在手机和平​​板电脑中的行为不同的原因可能是什么?谢谢你的回答。

【问题讨论】:

    标签: java android android-fragments mobile android-studio


    【解决方案1】:

    我在使用 Lollipop 的设备上遇到了完全相同的问题。我唯一能想到的就是在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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 2022-07-19
      • 1970-01-01
      • 2011-06-25
      相关资源
      最近更新 更多