Android開源日歷控件Caldroid的使用

armandolockwood 8年前發布 | 53K 次閱讀 Android開發 移動開發

來自: http://www.jcodecraeer.com//a/anzhuokaifa/androidkaifa/2014/0314/1592.html


Caldroid是一個以月為單位展示日期的日歷控件,主要功能在一個Fragment中。Caldroid既可以以Fragment的形式嵌套在布局中,也可以作為dialog fragment以對話框的形式展示出來。可以通過左右滑動切換月份。

Caldroid可以隨意自定義屬性,為了支持更多國家的實際情況,可以指定一周的開始時間,默認周日為一周的開始。Caldroid支持android2.2及以上版本。

項目配置:

要使用Caldroid,你可以參考源碼中附帶的demo,以便熟悉該library是如何工作的。

如果你要使用Caldroid,將Caldroid作為library導入到你的工程中,由于Caldroid使用了兼容包android-support-v4.jar,可能會因為與你項目中的android-support-v4.jar版本不一樣而導致編譯錯誤,如果出現這種情況,將Caldroid中的jar替換成你項目中的jar。

使用方法:

兩種使用方式,一種是嵌入式的Fragment,一種是dialog式的。

如果是嵌入到activity中:

CaldroidFragment caldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
Calendar cal = Calendar.getInstance();
args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
caldroidFragment.setArguments(args);
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.calendar1, caldroidFragment);
t.commit();

不僅如此,你還可以將該Fragment作為子fragment嵌入到另外一個Fragment中。

Caldroid可以在arguments中傳遞很多參數:

public final static String DIALOG_TITLE = "dialogTitle";
public final static String MONTH = "month";
public final static String YEAR = "year";
public final static String SHOW_NAVIGATION_ARROWS = "showNavigationArrows";
public final static String DISABLE_DATES = "disableDates";
public final static String SELECTED_DATES = "selectedDates";
public final static String MIN_DATE = "minDate";
public final static String MAX_DATE = "maxDate";
public final static String ENABLE_SWIPE = "enableSwipe";
public final static String START_DAY_OF_WEEK = "startDayOfWeek";
public final static String SIX_WEEKS_IN_CALENDAR = "sixWeeksInCalendar";
public final static String ENABLE_CLICK_ON_DISABLED_DATES = "enableClickOnDisabledDates";

定義一周的開始,代碼如下:

Bundle args = new Bundle();
args.putInt(CaldroidFragment.START_DAY_OF_WEEK, CaldroidFragment.TUESDAY); // Tuesday
caldroidFragment.setArguments(args);

如果想知道用戶點擊了處于disabled狀態下的日期:

Bundle args = new Bundle();
args.putInt(CaldroidFragment.ENABLE_CLICK_ON_DISABLED_DATES, true);
caldroidFragment.setArguments(args);

如果是想用對話框的形式使用,那么你還可以指定對話框的標題,代碼如下:

CaldroidFragment dialogCaldroidFragment = CaldroidFragment.newInstance("Select a date", 3, 2013);
dialogCaldroidFragment.show(getSupportFragmentManager(),"TAG");

Caldroid提供了很多定義日歷字體和背景顏色等樣式的方法,比如:

// You can use any of below methods to set background colors
public void setBackgroundResourceForDates(HashMap<Date, Integer> backgroundForDateMap);
public void setBackgroundResourceForDateTimes(HashMap<DateTime, Integer> backgroundForDateTimeMap);
public void setBackgroundResourceForDate(int backgroundRes, Date date);
public void setBackgroundResourceForDateTime(int backgroundRes, DateTime dateTime);
// Below methods is to set text color
public void setTextColorForDates(HashMap<Date, Integer> textColorForDateMap);
public void setTextColorForDateTimes(HashMap<DateTime, Integer> textColorForDateTimeMap);
public void setTextColorForDate(int textColorRes, Date date);
public void setTextColorForDateTime(int textColorRes, DateTime dateTime);

要使用這些方法,你需要在資源文件定義好顏色或者是drawable

caldroidFragment.setBackgroundResourceForDate(R.color.blue, blueDate);
caldroidFragment.setBackgroundResourceForDate(R.color.green, greenDate);
caldroidFragment.setTextColorForDate(R.color.white, blueDate);
caldroidFragment.setTextColorForDate(R.color.white, greenDate);

設置了一些參數之后需要調用refreshView()方法才能生效。

 

顯示或者隱藏切換月份的左右箭頭

public void setShowNavigationArrows(boolean showNavigationArrows);

是否允許左右滑動

public void setEnableSwipe(boolean enableSwipe);

指定日期(跳轉日期)

public void moveToDate(Date date);
public void moveToDateTime(DateTime dateTime);

實現監聽選中某個日期的事件

final CaldroidListener listener = new CaldroidListener() {
    @Override
    public void onSelectDate(Date date, View view) {
        Toast.makeText(getApplicationContext(), formatter.format(date),
                Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onChangeMonth(int month, int year) {
        String text = "month: " + month + " year: " + year;
        Toast.makeText(getApplicationContext(), text,
                Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onLongClickDate(Date date, View view) {
        Toast.makeText(getApplicationContext(),
                "Long click " + formatter.format(date),
                Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onCaldroidViewCreated() {
        Toast.makeText(getApplicationContext(),
                "Caldroid view is created",
                Toast.LENGTH_SHORT).show();
    }
};
caldroidFragment.setCaldroidListener(listener);

https://github.com/roomorama/Caldroid

 本文由用戶 armandolockwood 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!