編輯:關於 Android 程式設計
日曆控件常用於日期選擇、活動報名、行事提醒等情境。Android 中可透過自訂 View 或第三方庫實作月曆畫面。以下以 CalendarPickerView 為例,整理常見用法。
Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);
Calendar lastYear = Calendar.getInstance();
lastYear.add(Calendar.YEAR, -1);
calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
calendar.init(lastYear.getTime(), nextYear.getTime());
calendar.init(lastYear.getTime(), nextYear.getTime())
.inMode(CalendarPickerView.SelectionMode.SINGLE);
ArrayList<Date> dates = new ArrayList<Date>();
calendar.init(new Date(), nextYear.getTime())
.inMode(CalendarPickerView.SelectionMode.MULTIPLE)
.withSelectedDates(dates);
ArrayList<Date> dates = new ArrayList<Date>();
calendar.init(new Date(), nextYear.getTime())
.inMode(CalendarPickerView.SelectionMode.RANGE)
.withSelectedDates(dates);
Date selectedDate = calendar.getSelectedDate();
若要自訂每一天的呈現方式,可實作 DayViewAdapter。
public class SampleDayViewAdapter implements DayViewAdapter {
@Override
public void makeCellView(CalendarCellView parent) {
View layout = LayoutInflater.from(parent.getContext())
.inflate(R.layout.day_view_custom, null);
parent.addView(layout);
parent.setDayOfMonthTextView(
(TextView) layout.findViewById(R.id.day_view));
}
}
public class SampleDecorator implements CalendarCellDecorator {
@Override
public void decorate(CalendarCellView cellView, Date date) {
String dateString = Integer.toString(date.getDate());
SpannableString string = new SpannableString(dateString + "\ntitle");
string.setSpan(
new RelativeSizeSpan(0.5f),
0,
dateString.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
cellView.getDayOfMonthTextView().setText(string);
}
}





日曆控件的重點在於日期範圍、選擇模式與樣式自訂。若專案需要高度客製化,可考慮自繪月曆;若重視開發效率,則可使用成熟的事件日曆庫。
Android 手勢密碼元件詳解:開源庫用法與底層實作
簡介 最近專案需要用到手勢解鎖,翻了一些 demo 都覺得要改的地方太多,索然自己包了一層。這個開源庫主要提供這些能力: 支援手勢繪製與密碼保存,解鎖時自動比對並回報結
嵌入式 Linux 與 Android 學習路線圖:從零開始建立系統開發能力
寫給正在迷惘的學習者很多人在求學階段都會擔心:現在學的東西,未來真的用得上嗎?其實,正規教育訓練出的基礎能力,例如數學、程式設計、電子電路與自學方法,往往會在日後的職涯中
Android 數據解析全攻略:JSON 入門與 Gson、org.json、Jackson 三大方案詳解
前言 上一篇我們詳細介紹了 XML 及其 DOM、SAX、PULL 三種解析方式並做了對比,這次換個主角,聊聊同樣身為主流數據交換格式的 JSON! 定義 JavaScr
Android 直播視頻技術探究(一):基礎知識大綱全解
一、前言 近來各種視頻直播 App 遍地開花、各處霸屏。對非技術人員而言,直播是一種潮流與娛樂方式;但作為技術人,我們除了體驗,更重要的是鑽研背後的技術。其實 Andro