CalendarView可点击安卓(CalendarView Clickable Android)

2019-06-27 00:33发布

我想开始当你点击在CalendarView日期的新的活动,但我的事件似乎并不火。 我已经设置点击为真,并指定一个onclick(无论是在XML)

这是XML:

     <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"
     android:clickable="true"      
     android:onClick="CalendarClick" />

     </RelativeLayout>

这是我的日历活动代码:

    public class CalendarActivity extends Activity 
    {


      @Override
      public void onCreate(Bundle savedInstanceState) 
      {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_calendar);
      }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) 
     {
      getMenuInflater().inflate(R.menu.activity_calendar, menu);
      return true;
     }

     public void CalendarClick(View view)
     {
      Intent myIntent = new Intent(CalendarActivity.this, MainActivity.class);
      CalendarActivity.this.startActivity(myIntent);
     }      
  }

Answer 1:

这工作得很好.....

 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

            }
        });
    }

这是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>


Answer 2:

使用该知道已经被点击的日期

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

        }
    });


Answer 3:

如果你想单击已选定的日期OnGlobalLayoutListener会做的伎俩。

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


文章来源: CalendarView Clickable Android