屏幕中有一个日历 View ,我想在同一屏幕上显示所选日期。我希望它随着用户更改所选日期而动态更改。
这在我的手机 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 在手机和平板电脑中表现不同的原因可能是什么?谢谢您的回答。
最佳答案
我在使用 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
}
关于java - CalendarView 始终返回当天,忽略用户选择的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31602849/