Android进阶之路Android开发Android开发

日历视图使用

2019-12-18  本文已影响0人  Lee_5566
image.png

目录

CalenderView

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

使用例子:

    <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文件:

<?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>

代码:

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用法示例

上一篇 下一篇

猜你喜欢

热点阅读