UI组件:日历视图(CalendarView)

日历组件(CalendarView)可以用于显示和选择日期,用户即可选择一个日期,也可通过触摸来滚动日历。

如果要监控该组件的日期改变,可调用CalendarView的setOnDataChangeListener()方法为该组件的点击事件添加事件监听器。

CalendarView的常见XML属性如下:

XML属性相关方法说明
android:firstDayOfWeeksetFirstDayOfWeek(int)设置每周的第一天,允许设置周一到周日任意一天作为每周的第一天
android:shownWeekCountsetShownWeekCount()设置该日历组件总共显示几个星期(目前已过时)
android:selectedWeekBackgroundColorsetSelectedWeekBackgroundColor(int)设置被选中周的背景色(目前已过时)
android:focusedMonthDateColorsetFocusedMonthDateColor(int)设置获得焦点的月份的日期文字的颜色(目前已过时)
android:unfocusedMonthDateColorsetUnfocusedMonthDateColor(int)设置没有焦点的月份的日期文字的颜色(目前已过时)

下面通过一个实例来介绍CalendarView的用法。
布局文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择您的生日:" />
    <CalendarView
        android:id="@+id/calendar_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:selectedWeekBackgroundColor="#1cb0e1" />
</LinearLayout>

主程序代码如下所示:

public class MainActivity extends AppCompatActivity {

    private CalendarView calendarView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        calendarView = (CalendarView) findViewById(R.id.calendar_view);
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this,"你的生日是"+year+"年"+(month+1)+"月"+dayOfMonth+"日",Toast.LENGTH_SHORT).show();
            }
        });
    }

效果图如下所示:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值