适用于Android的日历视图?

时间:2016-05-11 19:05:53

标签: android

我需要在我的Android应用中添加日历视图。我需要用户能够通过点击传统日历中每天显示一个月的一天来选择日期,并且他可以以一些简单的方式在月份之间导航,例如点击按钮或滑动。我对造型并不挑剔。此应用程序在智能手机格式设备上运行。

当我在Stack Overflow this question中搜索此内容时,首先出现,并且它表示除非您想编写自己的或使用第三方开源代码,否则不存在此类内容。但它是从4年前开始的。我已经看到更多最近的Stack Overflow帖子暗指日历视图但没有细节。现在,在2016年,现有的Android日历视图我可以直接进入我的应用程序,这是Android SDK的一部分吗?如果是这样,我在哪里可以看到使用它的例子?

1 个答案:

答案 0 :(得分:4)

CalendarView自2011年2月发布的API Level 11(Android 3.0)以来就已存在。

This sample app演示了它的基本用法。

<CalendarView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/calendar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
/***
  Copyright (c) 2013 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
 */

package com.commonsware.android.wc.calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.Toast;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemoActivity extends Activity implements
    OnDateChangeListener {
  CalendarView calendar=null;

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

    calendar=(CalendarView)findViewById(R.id.calendar);
    calendar.setOnDateChangeListener(this);
  }

  @Override
  public void onSelectedDayChange(CalendarView view, int year,
                                  int monthOfYear, int dayOfMonth) {
    Calendar then=new GregorianCalendar(year, monthOfYear, dayOfMonth);

    Toast.makeText(this, then.getTime().toString(), Toast.LENGTH_LONG)
         .show();
  }
}

CalendarView sample

并且,有许多third-party date pickers,其中一些使用日历比喻。