服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android开发之日历CalendarView用法示例

Android开发之日历CalendarView用法示例

2022-10-07 16:31水中鱼之1999 Android

这篇文章主要介绍了Android开发之日历CalendarView用法,简单分析了日历CalendarView组件的功能、属性设置方法、界面布局、事件监听等相关操作技巧,需要的朋友可以参考下

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

简介:

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

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

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

实例:

 Android开发之日历CalendarView用法示例

基本设置方法:

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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 来获得用户选择的日期的具体信息:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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程序设计有所帮助。

原文链接:https://blog.csdn.net/qq_43377749/article/details/84988290

延伸 · 阅读

精彩推荐