In this tutorial, we’ll be discussing the Calendar Widget using the CalendarView class in our Android Application.

在本教程中,我们将使用Android应用程序中的CalendarView类讨论Calendar Widget。

Android日历视图 (Android Calendar View)

As the name suggests, a Calendar View is used to display and select dates of the Calendar.

顾名思义,“日历视图”用于显示和选择日历的日期。

To add a CalendarView in the XML Layout do the following:

要在XML布局中添加CalendarView,请执行以下操作:

<CalendarViewandroid:id="@+id/calendarView"android:layout_width="wrap_content"android:layout_height="wrap_content"/>

This is how it looks in the Layout Design editor :

这是在“布局设计”编辑器中的外观:

When you’ll run the above application on your device, it’ll show the current date. By default, the Calendar shows the Jan 1, 1970, date.

当您在设备上运行上述应用程序时,它将显示当前日期。 默认情况下,日历显示1970年1月1日的日期。

android:maxDate and android:minDate are used to set a custom range on the calendar. The dates specified are of the format MM/dd/yyyy.

android:maxDateandroid:minDate用于在日历上设置自定义范围。 指定的日期格式为MM / dd / yyyy。

To do the same in Java we use setMaxDate() and setMinDate() methods passing the long instance. The getters methods are available for the same.

为了在Java中执行相同的操作,我们使用setMaxDate()setMinDate()方法传递long实例。 吸气方法同样适用。

To set the current date we do setDate(long date) on the CalendarView instance.

要设置当前日期,我们在CalendarView实例上执行setDate(long date)

The setDate method has another form: setDate(long date, boolean animate, boolean center).
By default the second and third parameters are true. When you select a new date it animates to it.

setDate方法具有另一种形式: setDate(long date, boolean animate, boolean center)
默认情况下,第二个和第三个参数为true。 当您选择一个新的日期时,它会动画化。

To change the date and week text style we use the attributes:
android:dateTextAppearance and android:weekTextAppearance or their equivalent setters in Java.

要更改日期和星期文本样式,我们使用以下属性:
android:dateTextAppearanceandroid:weekTextAppearance或它们在Java中的等效设置器。

The CalendarView consists of the following listener:

CalendarView由以下侦听器组成:

calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {@Overridepublic void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {}});

This gets triggered whenever the date is changed by the user.

每当用户更改日期时,都会触发此事件。

  • i = year我=年
  • i1 = monthi1 =月
  • i2 = dayi2 =天

In the following section, we’ll create an android application with a custom theme and add a custom range on the CalendarView along with showing the difference between animation and non-animation date changes.

在下一节中,我们将创建一个具有自定义主题的android应用程序,并在CalendarView上添加自定义范围,并显示动画和非动画日期更改之间的差异。

项目结构 (Project Structure)

码 (Code)

In the styles.xml file add the following three styles:

在styles.xml文件中,添加以下三种样式:

<style name="CalenderViewCustom" parent="Theme.AppCompat"><item name="colorAccent">@android:color/holo_blue_dark</item><item name="colorPrimary">@android:color/darker_gray</item><item name="android:textColorPrimary">@color/colorPrimary</item></style><style name="CalenderViewDateCustomText" parent="android:TextAppearance.DeviceDefault.Small"><item name="android:textColor">@android:color/holo_orange_dark</item></style><style name="CalenderViewWeekCustomText" parent="android:TextAppearance.DeviceDefault.Small"><item name="android:textColor">@android:color/holo_green_dark</item></style>

android:textColorPrimary by default is white. This color is set on the month date and the left and right indicators.

android:textColorPrimary默认为白色。 该颜色设置在月份日期以及左右指示符上。

The code for the activity_main.xml is given below:

下面给出了activity_main.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"xmlns:app="https://schemas.android.com/apk/res-auto"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><CalendarViewandroid:id="@+id/calendarView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:dateTextAppearance="@style/CalenderViewDateCustomText"android:theme="@style/CalenderViewCustom"android:weekDayTextAppearance="@style/CalenderViewWeekCustomText"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/btnWithAnim"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="16dp"android:text="With\nAnim"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toStartOf="@+id/btnWithoutAnim"app:layout_constraintHorizontal_bias="0.5"app:layout_constraintStart_toEndOf="@+id/btnRange" /><Buttonandroid:id="@+id/btnWithoutAnim"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Without\nAnim"app:layout_constraintBaseline_toBaselineOf="@+id/btnWithAnim"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.5"app:layout_constraintStart_toEndOf="@+id/btnWithAnim" /><Buttonandroid:id="@+id/btnRange"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Custom\nRange"app:layout_constraintEnd_toStartOf="@+id/btnWithAnim"app:layout_constraintHorizontal_bias="0.5"app:layout_constraintHorizontal_chainStyle="spread"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="@+id/btnWithAnim" /></android.support.constraint.ConstraintLayout>

We’ve added all the custom styles in the layout above.

我们在上面的布局中添加了所有自定义样式。

The three Buttons are chained in the Constraint Layout.

这三个按钮链接在“ 约束布局”中 。

The code for the MainActivity.java is given below:

MainActivity.java的代码如下:

package com.journaldev.androidcalendarview;import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.Toast;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;public class MainActivity extends AppCompatActivity implements View.OnClickListener {Calendar calendar;CalendarView calendarView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);calendar = Calendar.getInstance();calendar.set(Calendar.MONTH, Calendar.NOVEMBER);calendar.set(Calendar.DAY_OF_MONTH, 9);calendar.set(Calendar.YEAR, 2012);calendar.add(Calendar.DAY_OF_MONTH, 1);calendar.add(Calendar.YEAR, 1);calendarView = findViewById(R.id.calendarView);Button btnRange = findViewById(R.id.btnRange);btnRange.setOnClickListener(this);Button btnWithoutAnim = findViewById(R.id.btnWithoutAnim);btnWithoutAnim.setOnClickListener(this);Button btnWithAnim = findViewById(R.id.btnWithAnim);btnWithAnim.setOnClickListener(this);calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {@Overridepublic void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {String msg = "Selected date Day: " + i2 + " Month : " + (i1 + 1) + " Year " + i;Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();}});}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.btnWithAnim:calendarView.setDate(calendar.getTimeInMillis(), true, true);break;case R.id.btnWithoutAnim:calendar.set(Calendar.DAY_OF_MONTH, 12);calendar.set(Calendar.YEAR, 2016);calendar.add(Calendar.MONTH, Calendar.MARCH);calendarView.setDate(calendar.getTimeInMillis(), false, false);break;case R.id.btnRange:Calendar calendar = Calendar.getInstance();calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));long endOfMonth = calendar.getTimeInMillis();calendar = Calendar.getInstance();calendar.set(Calendar.DATE, 1);calendar.set(Calendar.HOUR_OF_DAY, 0);long startOfMonth = calendar.getTimeInMillis();calendarView.setMaxDate(endOfMonth);calendarView.setMinDate(startOfMonth);String minDateString = new SimpleDateFormat("MM/dd/yyyy").format(new Date(calendarView.getMinDate()));String maxDateString = new SimpleDateFormat("MM/dd/yyyy").format(new Date(calendarView.getMaxDate()));Toast.makeText(getApplicationContext(), "MMDDYYYY Min date - " + minDateString + " Max Date is " + maxDateString, Toast.LENGTH_LONG).show();break;}}
}

calendar.getActualMaximum(Calendar.DATE) gets the end of the month for the current date.
We’ve used SimpleDateFormat to convert the dates into a custom format.

calendar.getActualMaximum(Calendar.DATE)获取当前日期的月底。
我们使用SimpleDateFormat将日期转换为自定义格式。

The output of the application in action is given below:

实际应用程序的输出如下:

In the first case, we animate to another date with animation. In the last case, the custom range shows only the July Month. The indicators are disabled.

在第一种情况下,我们使用动画为另一个日期设置动画。 在最后一种情况下,自定义范围仅显示七月。 指示灯已禁用。

This brings an end to this tutorial. You can download the project from the link below:

本教程到此结束。 您可以从下面的链接下载项目:

AndroidCalendarViewAndroid日历查看
Github Project LinkGithub项目链接

翻译自: https://www.journaldev.com/22321/android-calendar-view

Android日历视图相关推荐

  1. android 日历翻页动画,Android开源库合集:轻松实现Android动态,炫目:日历效果...

    前言: 了解过那种动态,炫目的日历效果吗?你知道是怎么 操作的嘛?是否想过,用UI就可以实现,对,也许你说的对,不过UI只是都是动态效果的一部分.那么今天用Annroid开源库,来告诉你android ...

  2. Android学习笔记(27):日历视图Calendar

    日历视图CalendarView可用于显示和选择日期. 可以调用setOnDateChangedListener()方法绑定事件监听器. 常用XML属性和相关方法: XML属性 相关方法 说明 and ...

  3. android 日历图,Android中的自定义日历视图

    我正在为我的 Android应用程序构建一个自定义日历视图,允许您在几个月之间滑动.我已经创建了自定义日历方块视图,我已将其嵌入到自定义日历月视图中,并且所有内容都在1个月的范围内完美运行. 不幸的是 ...

  4. android 横版日历,GitHub - hosle/HoVerticalCalendarView: Android 垂直滑动的日历视图

    HoVerticalCalendarView Author : hosle Created in 14th June 2018 Feature 支持连续月份的上下自由滚动 支持每天日期下添加任务计数标 ...

  5. 日历视图的XML属性

    日历视图的XML属性 : -- 设置样式 : android:dateTextAppearance, 设置日期文字显示样式; -- 设置首日 : android:firstDayOfWeek, 设置星 ...

  6. android 日历_适用于Android的十大最佳日历应用

    android 日历 Looking for the best calendar apps for Android? You're in the right place! Let's get righ ...

  7. android++日历示例,Android控件之CalendarView 日历对话框(示例代码)

    在Android 3.0中新增的日历视图控件可以显示网格状的日历内容,android.widget.CalendarView是从android.widget.FrameLayout中继承. Calen ...

  8. Android 日历开发教程[四]

    这节开始制作 Activity 视图. 视图的设计,在原理上可以借鉴 HTML 设计,目前大家的共识是内容与样式分离,也就是内容在 HTML 文件中定义,样式在 CSS 文件中对应. 同样,Andro ...

  9. ios日历视图实现日期输入

    在视图控制器上,触摸textfield,打开的不是虚拟键盘,也不是datepicker,也不要actionsheet,要一个类似html上的日历输入框. 这类控件有很多开源的,但目标不是我想要的.参考 ...

最新文章

  1. 2020 IEEE冯诺依曼奖得主:Michael Jordan --机器学习领域泰斗级人物
  2. .ajax显示加载动画,jQuery Ajax 加载数据时异步显示加载动画
  3. Ansible-playbook简单应用的几个实例
  4. xuggler 中文开发_Xuggler开发教程
  5. CSS Framework 960 Grid System (收)
  6. CentOS7 最小化安装工具包精简版本
  7. C#简单的生成随机数
  8. Json字符串和Java对象互相转换
  9. Codeforces 712C Memory and De-Evolution
  10. PHPCURL直接访问JSONRPC服务
  11. 升压和升降压拓扑中IDC与IO的关系推导 // 《精通开关电源设计》P41式2-2
  12. MIDI文件基础及使用Python库mido操作MIDI文件
  13. win10系统下摄像头无法打开的解决方法
  14. 计算机专业就业发展现状,计算机专业就业形势分析
  15. html 隐藏广告代码,js漂浮广告原理 js或者CSS带关闭的漂浮广告代码
  16. 优质的计算机专业书籍有哪些?
  17. Allegro使用总结-查看Layout基本操作:
  18. 黑客入侵应急分析手工排查
  19. java第二课——IDEA介绍
  20. 面由 AI 生|虚拟偶像“捏脸”技术解析

热门文章

  1. Ajax Post请求实例
  2. Perforce-Server迁移
  3. Python写入文件,但是发现文件为空,竟然未写入!
  4. 一扬开源新闻静态生成系统V1.0发布
  5. [转载] python中@property和property函数使用
  6. [转载] NumPy 基本操作(ndarray通用函数 / 常用函数)
  7. [转载] wikipedia 维基百科 语料 获取 与 提取 处理 by python3.5
  8. [转载] java中对数组进行排序_如何在Java中对数组排序
  9. Echarts Y轴min显示奇葩问题(做此记录)
  10. 高通HAL层之Sensor HAL