android-times-square的使用

本文简单介绍了Square出品的android-times-square的使用。

一,在gradle里添加依赖

compile 'com.squareup:android-times-square:1.6.5@aar'

二,布局文件如下




    

    


三,activity中代码如下

package liubo.is.handsome.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.squareup.timessquare.CalendarPickerView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import liubo.is.handsome.R;
import liubo.is.handsome.base.BaseActivity;
import liubo.is.handsome.utils.LogUtils;


/**
 * Created by liubo on 2017/2/6.
 * Description:
 * 
 */

public class CalendarActivity extends BaseActivity {

    TextView calendarTip;
    CalendarPickerView calendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        initView();
    }

    private void initView() {
        calendarTip = (TextView) findViewById(R.id.calendar_tip);
        calendar = (CalendarPickerView) findViewById(R.id.calendarView);
        calendar.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(final Date date) {
                LogUtils.lb("onDateSelected = " + formatDate(date));
                final int size = calendar.getSelectedDates().size();
                if (size % 2 == 0) {
                    calendarTip.setText(calendarTip.getText() + " - " + formatDate(date) + " -- > " + size + " 天");
                } else {
                    calendarTip.setText("" + formatDate(date));
                }
            }

            @Override
            public void onDateUnselected(Date date) {
//                LogUtils.lb("onDateUnselected = " + date);
            }
        });
        Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 1);

        Date today = new Date();
        calendar.init(today, nextYear.getTime()).withSelectedDate(today);

        //默认是只选择一个日期,如果想要选择多个日期,使用下面这行代码
        calendar.init(today, nextYear.getTime()).inMode(CalendarPickerView.SelectionMode.RANGE);
    }

    private String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str = format.format(date);
        return str;
    }
}

四,效果图如下

android-times-square的使用_第1张图片


------------------------------------------------------华丽丽的分割线----------------------------------------------------------------------------------------------------

五,附注

上面的例子使用的CalendarPickerView.SelectionMode.RANGE,有兴趣的可以尝试另外两种方式。日期选择模式是个枚举类

public enum SelectionMode {
    /**
     * Only one date will be selectable.  If there is already a selected date and you select a new
     * one, the old date will be unselected.
     */
    SINGLE,
    /** Multiple dates will be selectable.  Selecting an already-selected date will un-select it. */
    MULTIPLE,
    /**
     * Allows you to select a date range.  Previous selections are cleared when you either:
     * 
    *
  • Have a range selected and select another date (even if it's in the current range).
  • *
  • Have one date selected and then select an earlier date.
  • *
*/ RANGE }

===================================================================================================================

android-times-square的使用_第2张图片




你可能感兴趣的:(第三方库)