前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >日历视图使用

日历视图使用

作者头像
李小白是一只喵
发布2020-04-24 08:37:48
2.5K0
发布2020-04-24 08:37:48
举报
文章被收录于专栏:算法微时光算法微时光

image.png

目录

CalenderView

CalendarView是安卓自带的一个日历控件, 可以使用其开发手机日历的相关功能.

使用例子:

代码语言:javascript
复制
    <CalendarView
      android:id="@+id/calenderView"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
使用接口说明

接口

含义

setOnDataChangeListener()

添加监听事件,获取当前选择的日期

android:selectedWeekBackgroundColor="#aff"

日历的整体背景颜色

android:focusedMonthDateColor="#f00"

月份的背景色

android:weekSeparatorLineColor="#ff0"

星期的背景色

android:unfocusedMonthDateColor="#f9f"

被选中的日期背景色

实战

activity_main.xml文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <CalendarView
        android:id="@+id/calenderView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:selectedWeekBackgroundColor="#aff"
        android:focusedMonthDateColor="#f00"
        android:weekSeparatorLineColor="#ff0"
        android:unfocusedMonthDateColor="#f9f" />


</android.support.constraint.ConstraintLayout>

代码:

代码语言:javascript
复制
package com.example.user.calender;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    CalendarView calendarView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        calendarView = (CalendarView) findViewById(R.id.calenderView);
        //calendarView 监听事件
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange( CalendarView view, int year, int month, int dayOfMonth) {
                //显示用户选择的日期
                Toast.makeText(MainActivity.this,year + "年" + month + "月" + dayOfMonth + "日",Toast.LENGTH_LONG).show();
            }
        });
    }
}

运行效果:

image.png

参考

Android开发之日历CalendarView用法示例

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • CalenderView
    • 使用接口说明
    • 实战
    • 参考
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档