CalendarView Clickable Android

前端 未结 3 1849
日久生厌
日久生厌 2020-12-15 14:02

I am trying to start a new activity when you click on a date in CalendarView but my event doesn\'t seem to fire. I have set clickable to true and specified an onclick (both

相关标签:
3条回答
  • 2020-12-15 14:23

    This works fine.....

     public class MyActivity extends Activity{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             setContentView(R.layout.newact);
            CalendarView calendarView=(CalendarView) findViewById(R.id.calendarView1);
            calendarView.setOnDateChangeListener(new OnDateChangeListener() {
    
                @Override
                public void onSelectedDayChange(CalendarView view, int year, int month,
                        int dayOfMonth) {
                     Toast.makeText(getApplicationContext(), ""+dayOfMonth, 0).show();// TODO Auto-generated method stub
    
                }
            });
        }
    

    And this is xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <CalendarView
            android:id="@+id/calendarView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"  
            />
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-15 14:39

    OnGlobalLayoutListener will do the trick if you want to click already selected date.

    calendarView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
                @Override
                public void onGlobalLayout()
                {
                //your code here
                }
            });
    
    0 讨论(0)
  • 2020-12-15 14:44

    Use this to know which date has been clicked

    CalendarView calendarView=(CalendarView) findViewById(R.id.calendarView1);
        calendarView.setOnDateChangeListener(new OnDateChangeListener() {
    
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month,
                    int dayOfMonth) {
                 Toast.makeText(getApplicationContext(), ""+dayOfMonth, 0).show();// TODO Auto-generated method stub
    
            }
        });
    
    0 讨论(0)
提交回复
热议问题