当我向下滑动时,CalendarView关闭

时间:2015-04-26 12:38:37

标签: android calendar calendarview

我正在尝试设置日历视图,但我遇到了问题。我设置了android标准的CalendarView,它似乎行为正常,但有一个问题。当我向下滑动以查看下个月时,视图关闭,并选择实际日期。有人可以帮我解决这个问题吗?我想这不是很难解决,但我不能,而且我被困住了。

我在这里做了一切: http://www.thaicreate.com/mobile/android-calendarview.html(第二个例子) 甚至在这里: http://androiddhina.blogspot.in/2015/04/android-calender-view.html

这是标准实施。

1 个答案:

答案 0 :(得分:1)

实际上,当用户选择新日期或滚动到其他月份时,都会调用监听器。

这是解决方案:

public class CalendarViewActivity extends Activity {

    private static final String TAG = CalendarViewActivity.class.getSimpleName();

    private int mYear;
    private int mMonth;
    private int mDay;

    private long initialDate;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CalendarView calendar = new CalendarView(this);
        calendar.setOnDateChangeListener(mDateSetListener);

        // Save initial date of calendar
        initialDate = calendar.getDate();

        setContentView(calendar);
    }

    private CalendarView.OnDateChangeListener mDateSetListener = new CalendarView.OnDateChangeListener() {
        @Override public void onSelectedDayChange(CalendarView view, int year, int monthOfYear, int dayOfMonth) {

            if (view.getDate() == initialDate) {
                // Listener called but user have not selected date
                return;
            }

            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;

            view.getDate();

            String selectedDate = new StringBuilder().append(mMonth + 1).append("-").append(mDay).append("-")
                    .append(mYear).append(" ").toString();

            Bundle b = new Bundle();
            b.putString("dateSelected", selectedDate);

            //Add the set of extended data to the intent and start it
            Intent intent = new Intent();
            intent.putExtras(b);
            setResult(RESULT_OK,intent);
            finish();
        }
    };
}