android-times-square日期控件使用

1.github地址

https://github.com/square/android-times-square

2.引用依赖

compile 'com.squareup:android-times-square:1.6.5@aar'//时间选择器

3.使用

//明年
final Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);
//去年
final Calendar lastYear = Calendar.getInstance();
lastYear.add(Calendar.YEAR, -1);
//single
calendar.setCustomDayView(new DefaultDayViewAdapter());
calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList());
calendar.init(lastYear.getTime(), nextYear.getTime()) //
            .inMode(SelectionMode.SINGLE) //
            .withSelectedDate(new Date());//今天选中
//multi 可以选很多天
calendar.setCustomDayView(new DefaultDayViewAdapter());
Calendar today = Calendar.getInstance();
ArrayList<Date> dates = new ArrayList<Date>();
for (int i = 0; i < 5; i++) {
      today.add(Calendar.DAY_OF_MONTH, 3);
      dates.add(today.getTime());
}
 calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList());
calendar.init(new Date(), nextYear.getTime()) //今天到明年
            .inMode(SelectionMode.MULTIPLE) //
            .withSelectedDates(dates);//选中的天
//highlight加亮某段日期
Calendar c = Calendar.getInstance();
c.setTime(new Date());
calendar.setCustomDayView(new DefaultDayViewAdapter());
calendar.setDecorators(Collections<CalendarCellDecorator>emptyList());
//2000----2020
calendar.init(getDateWithYear(2000), getDateWithYear(2020)) // 20 years, enough to show performance failure
                .inMode(SelectionMode.SINGLE)
                .withSelectedDate(c.getTime());
//加亮部分
calendar.highlightDates(getHighlightedDaysForMonth(
 // Adds some highlighted days
                c.get(Calendar.MONTH) - 1,
                c.get(Calendar.MONTH),
                c.get(Calendar.MONTH) + 1));

private Date getDateWithYear(int year) {
    final Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, 0);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
  }
//range 设置范围,3---8
calendar.setCustomDayView(new DefaultDayViewAdapter());
Calendar today = Calendar.getInstance();
ArrayList<Date> dates = new ArrayList<Date>();
today.add(Calendar.DATE, 3);
dates.add(today.getTime());
today.add(Calendar.DATE, 5);
dates.add(today.getTime());
calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList());
        calendar.init(new Date(), nextYear.getTime()) //
            .inMode(SelectionMode.RANGE) //
            .withSelectedDates(dates);
//displayOnly只能显示
calendar.setCustomDayView(new DefaultDayViewAdapter());
calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList());
        calendar.init(new Date(), nextYear.getTime()) //
            .inMode(SelectionMode.SINGLE) //
            .withSelectedDate(new Date()) //
            .displayOnly();
//dialog 形式
String title = "I'm a dialog!";
showCalendarInDialog(title, R.layout.dialog);
dialogView.init(lastYear.getTime(), nextYear.getTime()) //
            .withSelectedDate(new Date());
//dialog的方法
private void showCalendarInDialog(String title, int layoutResId) {
    dialogView = (CalendarPickerView) getLayoutInflater().inflate(layoutResId, null, false);
    theDialog = new AlertDialog.Builder(this) //
        .setTitle(title)
        .setView(dialogView)
        .setNeutralButton("Dismiss", new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
          }
        })
        .create();
//固定尺寸        
theDialog.setOnShowListener(new DialogInterface.OnShowListener() {
      @Override public void onShow(DialogInterface dialogInterface) {
        Log.d(TAG, "onShow: fix the dimens!");
        dialogView.fixDialogDimens();
      }
    });
    theDialog.show();
  }
//customized自定义
showCalendarInDialog("Pimp my calendar!", R.layout.dialog_customized);
        dialogView.init(lastYear.getTime(), nextYear.getTime())
            .withSelectedDate(new Date());

//自定义
<com.squareup.timessquare.CalendarPickerView      xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/calendar_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp"
        android:scrollbarStyle="outsideOverlay"
        android:clipToPadding="false"
        android:background="@color/custom_background"   
        //天背景      app:tsquare_dayBackground="@drawable/custom_calendar_bg_selector"       
//选中字体颜色        app:tsquare_dayTextColor="@color/custom_calendar_text_selector"
        //分割线颜色
        app:tsquare_dividerColor="@color/transparent"      app:tsquare_titleTextColor="@color/custom_calendar_text_selector"
        //header字体颜色
   app:tsquare_headerTextColor="@color/custom_header_text"
        />
//SampleDayViewAdapter自定义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));
  }
}

动态设置dialog的大小和位置

 //设置大小  
 //要写在dialog.show();后面
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();  
layoutParams.width = 200;  
layoutParams.height = LayoutParams.WRAP_CONTENT;  
dialog.getWindow().setAttributes(layoutParams);  
//===
SelectDialog selectDialog = new SelectDialog(this,R.style.dialog);//创建Dialog并设置样式主题  
Window win = selectDialog.getWindow();  
LayoutParams params = new LayoutParams();  
params.x = -80;//设置x坐标  
params.y = -60;//设置y坐标  
win.setAttributes(params);  
selectDialog.setCanceledOnTouchOutside(true);//设置点击Dialog外部任意区域关闭Dialog  
selectDialog.show();  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值