api级别高于11的日期选择器的Android日历视图

api级别高于11的日期选择器的Android日历视图,android,dialog,calendar,Android,Dialog,Calendar,我想写一个有按钮的应用程序,当我点击按钮时,我想在对话框中显示一个日历,比如对话框中的日期选择器 对于日历,我尝试使用类似于newdatepickerdialog.OnDateSetListener()的构造函数。不可能这样做吗 api级别11之后,它支持日历视图。您好,请尝试下面的代码,希望它能帮助您: <Button android:id="@+id/birthday" android:layout_width="wrap_conte

我想写一个有按钮的应用程序,当我点击按钮时,我想在对话框中显示一个日历,比如对话框中的日期选择器

对于日历,我尝试使用类似于
newdatepickerdialog.OnDateSetListener()
的构造函数。不可能这样做吗


api级别11之后,它支持日历视图。

您好,请尝试下面的代码,希望它能帮助您:

  <Button
            android:id="@+id/birthday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

//此代码在对话框中显示日历,希望对您有所帮助

LayoutInflater inflater =(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.calendar, null,  false);
    CalendarView cv = (CalendarView) ll.findViewById(R.id.calendarView);
    cv.setBackgroundColor(Color.BLACK);


    cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month,
            int dayOfMonth) {
        // TODO Auto-generated method stub

        Log.d("date selected", "date selected " + year + " " + month + " " + dayOfMonth);
        int rows = showMapFragment(year, month, dayOfMonth);

        if (rows == 0){
            Toast.makeText(getApplicationContext(), year + "/" + (month + 1) +"/" + dayOfMonth + " No route to display", Toast.LENGTH_SHORT).show();
        }

        }
    });

    ll.findViewById(R.id.calendarView).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("calendar view", "calendar view clicked");
        }
    });

    Dialog calendarDialog = new Dialog(Main.this);
    calendarDialog.setContentView(ll);




    calendarDialog.setTitle("Pick a date to view your performance and route");
    calendarDialog.show();    
//以下是日历布局xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height= "400dp"
android:showWeekNumber="false"
android:clickable="true"
android:weekDayTextAppearance="@style/calendarStyle"
android:dateTextAppearance="@style/calendarStyle" 
/>

</LinearLayout>


感谢您的帮助。我用这个问题不符合我的要求。它显示的是日期选择器而不是日历。我看到的唯一区别是您将更改单击事件设置为触摸事件。我需要出示日历。thx.try this-if(android.os.Build.VERSION.SDK_INT>10){Datepicker.setCalendarViewShowed(true);}我在哪里调用
Datepicker.setCalendarViewShowed(true)方法?代码中没有声明日期选择器。我认为您应该创建一个新的xml,并在此xml中添加一个日历选择器,并使用充气器单击按钮调用它。。原因是我们使用DatePickerDialog.OnDateSetListener的对象,因此无法创建日历视图。我的想法是点击按钮使用充气机类,希望它能帮助你。我还没有尝试你的答案,这仍然适用于任何其他答案。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height= "400dp"
android:showWeekNumber="false"
android:clickable="true"
android:weekDayTextAppearance="@style/calendarStyle"
android:dateTextAppearance="@style/calendarStyle" 
/>

</LinearLayout>