当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发之日历CalendarView用法示例

Android开发之日历CalendarView用法示例

2019年07月23日  | 移动技术网IT编程  | 我要评论

3d肉蒲团 迅雷看看,廖静文的第二任丈夫,ww4399

本文实例讲述了android开发之日历calendarview用法。分享给大家供大家参考,具体如下:

简介:

1.calendarview是安卓自带的一个日历控件

2.在主活动中 通过设置setondatachangelistener() 来为其添加监听事件

可在其中获得 洪湖所选择的年月日的 详细信息

实例:

 

基本设置方法:

1. 日历的整体背景颜色 android:selectedweekbackgroundcolor="#aff"
2. 月份选择部分的背景色 android:focusedmonthdatecolor="#f00"
3. 显示星期的背景色 android:weekseparatorlinecolor="#ff0"
4. 被选中的日期的背景色 android:unfocusedmonthdatecolor="#f9f"

这里给出它的布局文件中的调用与配置:

<?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:gravity="center_horizontal"
  android:orientation="vertical">
    <textview
      android:text="please choose your birthday :"
      android:gravity="center"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textsize="15dp"
      android:typeface="monospace"/>
    <!--1.设置以星期二为每周第一天-->
    <!--2.设置该组件总共显示四个星期-->
    <!--3.并对该组件的星期尽心了定制-->
    <calendarview
      android:id="@+id/calenderview"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:firstdayofweek="3"
      android:shownweekcount="4"
      android:selectedweekbackgroundcolor="#aff"
      android:focusedmonthdatecolor="#f00"
      android:weekseparatorlinecolor="#ff0"
      android:unfocusedmonthdatecolor="#f9f">
    </calendarview>
</linearlayout>

在主活动中,为其添加监听事件后

可以通过 day month dayofmonth 来获得用户选择的日期的具体信息:

public class mainactivity extends activity {
  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_short).show();
      }
    });
  }
}

参考自疯狂android讲义

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网