日历视图可点击Android

日历视图可点击Android,android,android-layout,android-intent,android-activity,android-calendar,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:layou

当您在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>

使用此选项可了解已单击的日期

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

        }
    });
这很好用

 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>

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

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

选中
public void CalendarClick(View View)Toast something{}
它只在我在日期之外而不是在特定日期单击时才会烤。这会给我带来错误,如:此行有多个标记-标记“setOnDateChangeListener”上的语法错误,=此标记后应有-标记上的语法错误,在SelectedDayChange(CalendarView,int,int,int)上放置错误的构造和方法CalendarActivity类型必须重写或实现超类型方法。此外,我试图在xml中找到onDateChanged,但CalendarView似乎没有这样的属性。我认为您忘记添加这些行了“导入android.widget.CalendarView;导入android.widget.CalendarView.OnDateChangeListener;“对我来说,它只显示周名称。但它不显示月份日期。嗨,它工作正常,但它可以工作api 9及以上…或任何选项。
calendarView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
            @Override
            public void onGlobalLayout()
            {
            //your code here
            }
        });