CalendarView今天日期项目点击

时间:2013-10-24 10:59:13

标签: android calendarview

我在互联网上搜索了很多,但我无法找到正确的CalendarView解决方案点击今天的日期。

enter image description here

我需要将CalendarView用于活动应用。 setOnDateChangeListener适用于其他日期点击。

代码:

CalendarView calendarView=(CalendarView) findViewById(R.id.calendarView1);
    calendarView.setOnDateChangeListener(new OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month,
                int dayOfMonth) {
            Intent k = new Intent(GlavnaAktivnost.this, DatumDetalji.class);
            k.putExtra("godina", year);
            k.putExtra("mesec", month);
            k.putExtra("dan", dayOfMonth);
            startActivity(k);

        }
    });

我不太清楚我需要做什么以及最佳解决方案是什么? 我能够在堆栈上找到的答案是扩展CalendarView或绑定点击UI但我无法找到它的方法。你能举个例子吗?

TNX。

8 个答案:

答案 0 :(得分:2)

setOnDateChangeListener不会触发,但是OnGlobalLayoutListener会触发。 因此,如果您尝试:calendarView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){@Override public void onGlobalLayout() { } });

答案 1 :(得分:1)

CalendarView.java的回顾表明突出显示的行被一个消耗触摸事件的内部私有类所覆盖,因此OnClickListener()和OnTouchListener()都不起作用。

第一个(更难的)选项是制作您自己的CalendarView.java副本(称之为MyCustomCalendarView.java)并将其包含在您的项目中。在MyCustomCalendarView.java中,添加代码以注册OnTouchListener,如下所示:

private View.OnTouchListener touchListener;
@Override
public void setOnTouchListener( View.OnTouchListener otl) {
    touchListener = otl;
}

然后在私人课程WeeksAdapter的OnTouch()方法中,添加以下行:

@Override
 public boolean onTouch(View v, MotionEvent event) {
     if (mListView.isEnabled() && mGestureDetector.onTouchEvent(event)) {
         touchListener.onTouch(v, event);
     ...

然后可以按如下方式使用MyCustomCalendarView:

MyCustomCalendarView thisCalendar = (MyCustomCalendarView) rootView.findViewById(R.id.mycustom_calendar_view_layout_id);
thisCalendar.setOnTouchListener(myCustomListener);

另一种(更简单的)替代方法是在calendarView布局中添加一个按钮,并使用按钮的onClickListener(加上可能是一个标志变量)来检测是否已选择任何日期。在这种情况下,选择当前日期将与&#34相同;不选择日期"从设备的角度来看,您可以使用该事实来撰写今天的日期或其他自定义操作。

您可以使用datetimepicker-library的日期选择器布局来帮助您入门:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:gravity="center" 
    android:orientation="vertical" 
    android:background="@color/date_picker_view_animator" 
    android:layout_width="@dimen/date_picker_component_width" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="wrap_content" 
        android:layout_height="@dimen/selected_calendar_layout_height">
        <include layout="@layout/date_picker_selected_date" />
    </LinearLayout>

    <!-- this is the calendarView, add your own version here as you see fit -->
    <include layout="@layout/date_picker_view_animator" />

    <!-- this is the button to be added to this layout -->
    <View 
        android:background="@color/line_background" 
        android:layout_width="@dimen/date_picker_component_width" 
        android:layout_height="1.0dip" />
        <include layout="@layout/date_picker_done_button" />
</LinearLayout>

答案 2 :(得分:1)

请尝试以下方式:

//First set current day,month,year :

Calendar c = Calendar.getInstance();
int prevDay = c.get(Calendar.DAY_OF_MONTH);
int prevMonth = c.get(Calendar.MONTH);
int prevYear = c.get(Calendar.YEAR);
Date d = new Date(prevYear, prevMonth, prevDay);
c.setTime(d);

然后使用dateChangeListener:

CalendarView calendar = (CalendarView) findViewById(R.id.calendarViewCN);
calendar.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year,int month, int dayOfMonth) {
<b>boolean changed = changeUpdate(year, month, dayOfMonth);</b>
if (changed) {
Toast.makeText(getApplicationContext(),
"New Y: " + year + " M: " + month + " D: "+dayOfMonth, Toast.LENGTH_LONG).show();
}
}
});

现在声明changeUpdate():

private boolean changeUpdate(int curYear, int curMonth, int curDay) {
boolean changed = false;
if (curDay != prevDay || curMonth!=prevMonth || curYear!=prevYear) {
changed = true;
}

答案 3 :(得分:1)

不是直接的解决方案,但我最终做的是忽略OnDateChangeListener并添加一个SELECT按钮。不是我想要的解决方案,但它有效...

https://stackoverflow.com/a/33635580/1121497

答案 4 :(得分:1)

尝试使用下一种方式:

Date nowDate = new Date();

int mYear = nowDate.getYear();
int mMonth = nowDate.getMonth();
int mDay = nowDate.getDay();

CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView1);
calendarView.setOnDateChangeListener(new OnDateChangeListener() {

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month,
        int dayOfMonth) {

        mYear = year;
        mMonth = month;
        mDay = dayOfMonth;

        openGlavnaAktivnost();
    }
});


private void openGlavnaAktivnost(){

    Intent k = new Intent(GlavnaAktivnost.this, DatumDetalji.class);
    k.putExtra("godina", mYear);
    k.putExtra("mesec", mMonth);
    k.putExtra("dan", mDay);
    startActivity(k);
}

答案 5 :(得分:0)

试试这个:

CalendarView calendarView=(CalendarView) findViewById(R.id.calendarView1);
calendarView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(getDate());
        Intent k = new Intent(GlavnaAktivnost.this, DatumDetalji.class);
        k.putExtra("godina", calendar.get(Calendar.YEAR);
        k.putExtra("mesec", calendar.get(Calendar.MONTH);
        k.putExtra("dan", calendar.get(Calendar.DAY_OF_MONTH);
        startActivity(k);

    }
});

答案 6 :(得分:0)

适合我/ String:

    Intent intent = new Intent(MainActivity.this, DayActivity.class);
            intent.putExtra("year", String.valueOf(year) );
            intent.putExtra("mon", String.valueOf(month));
            intent.putExtra("day", String.valueOf(dayM));
            startActivity(intent);

或者需要在TextView

中将int转换为String到setText

答案 7 :(得分: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();
        }
    }
}