UI组件:日历视图(CalendarView)

来源:互联网 发布:知乎账号怎么注销 编辑:程序博客网 时间:2024/05/16 14:58

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

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

CalendarView的常见XML属性如下:

XML属性 相关方法 说明 android:firstDayOfWeek setFirstDayOfWeek(int) 设置每周的第一天,允许设置周一到周日任意一天作为每周的第一天 android:shownWeekCount setShownWeekCount() 设置该日历组件总共显示几个星期(目前已过时) android:selectedWeekBackgroundColor setSelectedWeekBackgroundColor(int) 设置被选中周的背景色(目前已过时) android:focusedMonthDateColor setFocusedMonthDateColor(int) 设置获得焦点的月份的日期文字的颜色(目前已过时) android:unfocusedMonthDateColor setUnfocusedMonthDateColor(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 0