CalendarView Clickable Android

时间:2012-08-14 09:12:49

标签: android android-layout android-intent android-activity android-calendar

我在CalendarView中点击日期时尝试启动新活动,但我的事件似乎没有触发。我已将clickable设置为true并指定了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);
     }      
  }

3 个答案:

答案 0 :(得分:15)

这很好用.....

 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>

答案 1 :(得分: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

        }
    });

答案 2 :(得分:0)

如果要单击已选择的日期,OnGlobalLayoutListener将执行此操作。

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