1

onSelectedDayChange 的问题是,当我打开日历视图并单击重点日时,什么也没有发生。例如,我想查看今天的详细信息。现在我打开日历视图,今天是重点。所以我必须点击另一天,然后点击今天查看详细信息。有什么技巧可以解决这个问题吗?

4

2 回答 2

0

CalendarView 派生自 View 可以生成 onClick() 事件。您可以尝试从视图中捕获 onClick() 事件。只是一个想法

于 2013-06-02T11:13:23.653 回答
0

我做了以下课程来解决这个问题。

public class CustomCalendarView extends CalendarView {

    private final String LOG_HEADER = "CAL:VW";
    private Date previousSelectedDate = new Date();
    private OnDateChangeListener listener;
    private CheckSameSelectedDateAsyncTask task = new CheckSameSelectedDateAsyncTask();

    public CustomCalendarView(Context context) {
        super(context);
    }

    public CustomCalendarView(Context context, AttributeSet attribute) {
        super(context, attribute);
    }

    public CustomCalendarView(Context context, AttributeSet attribute, int defStyle) {
        super(context, attribute, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if ((task.getStatus() == AsyncTask.Status.PENDING) || (task.getStatus() == AsyncTask.Status.FINISHED)) {
            task = new CheckSameSelectedDateAsyncTask();
            task.execute();
        }
        return false;
    }

    private void checkSameSelectedDate() {
        Date selectedDate = new Date(super.getDate());
        if (selectedDate.getDay() == previousSelectedDate.getDay() &&
                selectedDate.getMonth() == previousSelectedDate.getMonth() &&
                selectedDate.getYear() == previousSelectedDate.getYear()) {
            if (listener != null) {
                this.listener.onSameSelectedDayChange(this, selectedDate.getYear(), selectedDate.getMonth(), selectedDate.getDay());
            }
        }
        this.previousSelectedDate = selectedDate;
    }

    public void setSameSelectedDayChangeListener(OnDateChangeListener listener) {
        this.listener = listener;
    }

    public interface OnDateChangeListener extends CalendarView.OnDateChangeListener {
        void onSameSelectedDayChange(CalendarView view, int year, int month, int day);
    }

    private class CheckSameSelectedDateAsyncTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                //Note: Breaking point between 75 - 100
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            checkSameSelectedDate();
        }
    }
}
于 2017-07-21T09:14:11.877 回答