CalendarView today date item click

允我心安 提交于 2019-12-01 16:28:17

问题


I searched a lot on the internet and I couldn't succeed to find correct solution for CalendarView click on today date.

I need to use CalendarView for events app. setOnDateChangeListener works good for other day clicks.

Code:

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);

        }
    });

I dont understand well what I need to do and what is the best solution for this? Answers that I was abled to find on stack is to extend CalendarView or bind click on UI but I couldn't find a way with it. Can you give me some example?

Tnx.


回答1:


setOnDateChangeListener will not fire, but OnGlobalLayoutListener will. So if you try:

calendarView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
    @Override
    public void onGlobalLayout(){
    }
});



回答2:


A review of CalendarView.java indicates that the highlighted row is overrided by an internal private class which consumes the touch event, so neither OnClickListener() nor OnTouchListener() will work.

The first (harder) options is to make your own copy of CalendarView.java (call it MyCustomCalendarView.java) and include it into your project. Within MyCustomCalendarView.java, add code to register an OnTouchListener like so:

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

Then in the private class WeeksAdapter's OnTouch() method, add this line:

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

Then MyCustomCalendarView could be used as follows:

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

Another (easier) alternative is to add a button to the calendarView layout, and use the button's onClickListener (plus maybe a flag variable) to detect whether any date has been selected. In this case, selecting the current date will be the same as "no date selected" from the device's point of view, so you can use that fact to write in today's date, or other custom actions.

You can use datetimepicker-library's date picker layout to help you get started:

<?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>



回答3:


Not a direct solution but what I finally did was to ignore OnDateChangeListener and add a SELECT button. Not my desired solution either but it works...

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




回答4:


Try this with below way:

//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);

Then use 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();
}
}
});

Now declare the changeUpdate() :

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



回答5:


Try to use next way:

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);
}



回答6:


Try this:

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);

    }
});



回答7:


works for me/ 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);

OR need to convert int to String to setText in TextView




回答8:


I made the used the following class. It is a bit of a hack with the thread sleep, but one can increase the sleep time.

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();
        }
    }
}


来源:https://stackoverflow.com/questions/19564056/calendarview-today-date-item-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!