android-times-square的使用

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

一,在gradle里添加依赖

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

二,布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="liubo.is.handsome.activity.MainActivity">

    <TextView
        android:id="@+id/calendar_tip"
        android:layout_width="match_parent"
        android:layout_height="@dimen/textview_height"
        android:gravity="center"
        />

    <com.squareup.timessquare.CalendarPickerView
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"/>
</LinearLayout>

三,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;
    }
}

四,效果图如下



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

五,附注

上面的例子使用的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:
     * <ul>
     * <li>Have a range selected and select another date (even if it's in the current range).</li>
     * <li>Have one date selected and then select an earlier date.</li>
     * </ul>
     */
    RANGE
  }

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





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值