文章由來:學習一下日歷控件的實現,Android3.0以下的版本請查閱其他文章。
本文主要是介紹Android日歷視圖控件CalendarView相關的內容,然后在后面會給出一個簡單的Demo。
CalendarView繼承自FrameLayout
其他文章都引用的類似的java代碼示例如下:

public class MainActivity extends Activity implements OnClickListener, OnDateChangeListener { private CalendarView calendarView; private TextView tvCalendar, tvReminder, tvNote, tvMore; private ImageView ivNext; private ImageView ivPrevious; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); // initData(); // initUI(); } }
這部分代碼的工作主要在於加載layout,CalendarView被使用的時候有一些默認值,這里對默認值做了一點點改動,對界面進行美化。
xml代碼中CalendarView部分如下:

<LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="8" android:orientation="vertical" > <CalendarView android:id="@+id/calendarview" android:layout_width="fill_parent" android:layout_height="match_parent" android:focusedMonthDateColor="@color/whitesmoke" android:unfocusedMonthDateColor="@color/transparent" android:selectedWeekBackgroundColor="@color/transparent" android:showWeekNumber="false" android:weekSeparatorLineColor="@color/transparent" /> </LinearLayout>
實現的效果圖是這樣的:
以上代碼就引出了使用到了CalendarView類的屬性設置,看看在設置是用得較多的代碼:
公有方法 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
getDate()
Gets the selected date in milliseconds since January 1, 1970 00:00:00 in
getDefault() time zone.
|
|||||||||||
getDateTextAppearance()
Gets the text appearance for the calendar dates.
|
|||||||||||
getFirstDayOfWeek()
Gets the first day of week.
|
|||||||||||
getFocusedMonthDateColor()
Gets the color for the dates in the focused month.
|
|||||||||||
getMaxDate()
Gets the maximal date supported by this
CalendarView in milliseconds since January 1, 1970 00:00:00 in
getDefault() time zone.
|
|||||||||||
getMinDate()
Gets the minimal date supported by this
CalendarView in milliseconds since January 1, 1970 00:00:00 in
getDefault() time zone.
|
|||||||||||
getSelectedDateVerticalBar()
Gets the drawable for the vertical bar shown at the beginning and at the end of the selected date.
|
|||||||||||
getSelectedWeekBackgroundColor()
Gets the background color for the selected week.
|
|||||||||||
getShowWeekNumber()
Gets whether to show the week number.
|
|||||||||||
getShownWeekCount()
Gets the number of weeks to be shown.
|
|||||||||||
getUnfocusedMonthDateColor()
Gets the color for the dates in a not focused month.
|
|||||||||||
getWeekDayTextAppearance()
Gets the text appearance for the week day abbreviation of the calendar header.
|
|||||||||||
getWeekNumberColor()
Gets the color for the week numbers.
|
|||||||||||
getWeekSeparatorLineColor()
Gets the color for the separator line between weeks.
|
|||||||||||
isEnabled()
返回該視圖的活性狀態.由子類來解釋各自的活性狀態.
|
|||||||||||
onInitializeAccessibilityEvent(AccessibilityEvent event)
使用作為事件源的該視圖的信息初始化
AccessibilityEvent 事件.換句話說, 該視圖狀態的變化是觸發輔助事件的源頭.
|
|||||||||||
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
使用該視圖的信息初始化
AccessibilityNodeInfo 對象.
|
|||||||||||
setDate(long date)
Sets the selected date in milliseconds since January 1, 1970 00:00:00 in
getDefault() time zone.
|
|||||||||||
setDate(long date, boolean animate, boolean center)
Sets the selected date in milliseconds since January 1, 1970 00:00:00 in
getDefault() time zone.
|
|||||||||||
setDateTextAppearance(int resourceId)
Sets the text appearance for the calendar dates.
|
|||||||||||
setEnabled(boolean enabled)
設置視圖的可用狀態.由子類決定視圖的各可用狀態如何顯示.
|
|||||||||||
setFirstDayOfWeek(int firstDayOfWeek)
Sets the first day of week.
|
|||||||||||
setFocusedMonthDateColor(int color)
Sets the color for the dates of the focused month.
|
|||||||||||
setMaxDate(long maxDate)
Sets the maximal date supported by this
CalendarView in milliseconds since January 1, 1970 00:00:00 in
getDefault() time zone.
|
|||||||||||
setMinDate(long minDate)
Sets the minimal date supported by this
CalendarView in milliseconds since January 1, 1970 00:00:00 in
getDefault() time zone.
|
|||||||||||
setOnDateChangeListener(CalendarView.OnDateChangeListener listener)
Sets the listener to be notified upon selected date change.
|
|||||||||||
setSelectedDateVerticalBar(Drawable drawable)
Sets the drawable for the vertical bar shown at the beginning and at the end of the selected date.
|
|||||||||||
setSelectedDateVerticalBar(int resourceId)
Sets the drawable for the vertical bar shown at the beginning and at the end of the selected date.
|
|||||||||||
setSelectedWeekBackgroundColor(int color)
Sets the background color for the selected week.
|
|||||||||||
setShowWeekNumber(boolean showWeekNumber)
Sets whether to show the week number.
|
|||||||||||
setShownWeekCount(int count)
Sets the number of weeks to be shown.
|
|||||||||||
setUnfocusedMonthDateColor(int color)
Sets the color for the dates of a not focused month.
|
|||||||||||
setWeekDayTextAppearance(int resourceId)
Sets the text appearance for the week day abbreviation of the calendar header.
|
|||||||||||
setWeekNumberColor(int color)
Sets the color for the week numbers.
|
|||||||||||
setWeekSeparatorLineColor(int color)
Sets the color for the separator line between weeks.
|
一般常用的方法摘借(非本人翻譯,感謝翻譯的朋友們):

long getDate(); //獲取從1970年,1月1日,0點0分0秒到現在的毫秒數,因為返回是long型最終只能截止到2038年 int getFirstDayOfWeek(); //獲取當天是本周的第幾天,Android123提示返回的定義在java.util.Calendar類中,比如Calendar.Monday為星期一,定義值為2。 long getMaxDate(); //獲取CalendarView支持1970年到那天的最大天數 long getMinDate(); //獲取CalendarView支持1970年到那天的最小天數 boolean getShowWeekNumber(); //獲取是否顯示星期號 boolean isEnabled(); //是否顯示本日歷視圖 public void setDate(long date, boolean animate, boolean center); //設置選擇日期到1970年的描述 void setDate(long date); //設置選擇的日期描述到1970年 void setEnabled(boolean enabled); //設置是否啟用視圖 void setFirstDayOfWeek(int firstDayOfWeek); //設置本周起始天數 void setMaxDate(long maxDate); void setMinDate(long minDate); void setOnDateChangeListener(CalendarView.OnDateChangeListener listener); //日歷視圖修改的接口,這個介紹看下面。 void setShowWeekNumber(boolean showWeekNumber); //設置是否顯示周號
如果有了解更多,繼續補充在這里