android-times-square的使用

来源:互联网 发布:sql alter column 编辑:程序博客网 时间:2024/05/04 15:45

本文简单介绍了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  }

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





0 0
原创粉丝点击