簡體   English   中英

CalendarView總是返回當前日期,而忽略用戶選擇了什么

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

屏幕上有一個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在手機和平​​板電腦上的行為不同可能是什么原因? 謝謝您的回答。

我在使用棒棒糖的設備上遇到了完全相同的問題。 我唯一想到的就是在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