CalendarView today date item click

前端 未结 8 1164
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 10:49

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

相关标签:
8条回答
  • 2021-01-18 10:51

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

    calendarView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
        @Override
        public void onGlobalLayout(){
        }
    });
    
    0 讨论(0)
  • 2021-01-18 11:04

    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);
    }
    
    0 讨论(0)
  • 2021-01-18 11:05

    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>
    
    0 讨论(0)
  • 2021-01-18 11:07

    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

    0 讨论(0)
  • 2021-01-18 11:08

    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);
    
        }
    });
    
    0 讨论(0)
  • 2021-01-18 11:13

    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

    0 讨论(0)
提交回复
热议问题