开源框架android-times-square实现日历选择日期

本文介绍了一个名为android-times-square的开源框架,该框架提供了一种简单的方法来在Android应用中集成日历时间选择器。通过自定义对话框和实用工具类,开发者可以轻松地将美观的日历组件整合到他们的应用程序中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.概述

今天给大家介绍一个开源框架android-times-square,这是干嘛的呢?当我们在项目中需要使用日历选择时间的时候,就可以用到了,下面开始介绍。

二.使用

1.使用一

地址如下
https://github.com/square/android-times-square

我们先看下效果图,再讲解如何实现
这里写图片描述

1.CustomerDialog

public class CustomerDialog {

    private Activity context;
    private int res;
    private CustomerViewInterface listener;
    private AlertDialog dlg;

    public CustomerDialog(Activity context, int res) {
        this.context = context;
        this.res = res;
    }

    /**
     * 调用这个构造方法之后必须调用init方法
     */
    public CustomerDialog() {

    }

    public void init(Activity context, int res) {
        this.context = context;
        this.res = res;
    }

    /**
     * 在调用这个方法之前最好先调用setOnCustomerViewCreated来控制dialog自定义界面上的内容
     */
    public void showDlg() {
        dlg = new AlertDialog.Builder(context).create();
        dlg.setCanceledOnTouchOutside(true);
        dlg.setCancelable(true);
        dlg.show();
        Window window = dlg.getWindow();
        // 下面的清除flag主要是为了在dialog中有editText时弹出软件盘所用。
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        window.setContentView(res);
        if (listener != null) {
            listener.getCustomerView(window, dlg);
        }
    }

    public void setDlgIfClick(boolean ifClick) {
        if (dlg != null) {
            dlg.setCancelable(ifClick);
            dlg.setCanceledOnTouchOutside(ifClick);
        }
    }

    public void dismissDlg() {
        if (dlg != null) {
            dlg.dismiss();
            context = null;
            listener = null;
            dlg = null;
        }
    }

    public AlertDialog getDlg() {
        return dlg;
    }

    public interface CustomerViewInterface {

        void getCustomerView(final Window window, final AlertDialog dlg);
    }

    public interface ClickCallBack {

        void onOk(CustomerDialog dlg);

        void onCancel(CustomerDialog dlg);
    }

    public void setOnCustomerViewCreated(CustomerViewInterface listener) {
        this.listener = listener;
    }
}

2.Util

public class Util {
    public static CustomerDialog showChooseDateDialog(final Activity context, final String title, final String okText, final String cancelText, final CustomerDialog.ClickCallBack clickCallBack, final TextView view) {
        final CustomerDialog customerDialog = new CustomerDialog(context, R.layout.dialog_choosedate_layout);
        customerDialog.setOnCustomerViewCreated(new CustomerDialog.CustomerViewInterface() {
            @Override
            public void getCustomerView(Window window, AlertDialog dlg) {
                TextView tv_title = (TextView) window.findViewById(R.id.title);
                Button left_button = (Button) window.findViewById(R.id.left_button);
                Button right_button = (Button) window.findViewById(R.id.right_button);
                final CalendarPickerView pickerView = (CalendarPickerView) window.findViewById(R.id.calendar_picker);
                Calendar lastYear = Calendar.getInstance();
                lastYear.add(Calendar.DAY_OF_WEEK, -1);
                Calendar currentYear = Calendar.getInstance();
                currentYear.add(Calendar.DAY_OF_WEEK, 0);
                Calendar nextYear = Calendar.getInstance();
                nextYear.add(Calendar.DAY_OF_WEEK, 1);
                pickerView.init(lastYear.getTime(), nextYear.getTime()).withSelectedDate(new Date());
                //点击范围之外时的提示
                pickerView.setOnInvalidDateSelectedListener(new CalendarPickerView.OnInvalidDateSelectedListener() {
                    @Override
                    public void onInvalidDateSelected(Date date) {
                        Toast.makeText(context, "非法的日期", Toast.LENGTH_SHORT).show();
                    }
                });
                if (!TextUtils.isEmpty(title)) {
                    tv_title.setText(title);
                } else {
                    tv_title.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(cancelText)) {
                    left_button.setText(cancelText);
                }
                left_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        clickCallBack.onCancel(customerDialog);
                    }
                });
                if (!TextUtils.isEmpty(okText)) {
                    right_button.setText(okText);
                }
                right_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        clickCallBack.onOk(customerDialog);
                        long time = pickerView.getSelectedDate().getTime();
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        String result = format.format(time);
                        view.setText(result);
                    }
                });
            }
        });
        customerDialog.showDlg();
        return customerDialog;
    }
}

3.MainActivity

  Utils.showChooseDateDialog(MainActivity.this, "请选择日期", "确定", "取消", new CustomerDialog.ClickCallBack() {
                @Override
                public void onOk(CustomerDialog dlg) {
                    dlg.dismissDlg();
                }
                @Override
                public void onCancel(CustomerDialog dlg) {
                    dlg.dismissDlg();
                }
            },et_date);

最后我们看布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/bg_title_custom_dialog" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="12dp"
            android:text="选择日期"
            android:textColor="#000000"
            android:textSize="18sp" />

        <ScrollView
            android:id="@+id/sv_message"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="50dp"
            android:layout_marginTop="50dp"
            android:lineSpacingExtra="5dp"
            android:lineSpacingMultiplier="1.5"
            android:fadingEdge="none"
            android:overScrollMode="never"
            android:scrollbars="none" >

          <com.squareup.timessquare.CalendarPickerView
              android:id="@+id/calendar_picker"
              android:layout_width="wrap_content"
              android:overScrollMode="never"
              android:fadingEdge="none"
              android:scrollbars="none"
              android:layout_height="wrap_content">
          </com.squareup.timessquare.CalendarPickerView>

        </ScrollView>

        <LinearLayout
            android:id="@+id/bottom_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="12dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:gravity="center"
            android:orientation="horizontal"
            android:weightSum="2.5" >

            <Button
                android:id="@+id/left_button"
                android:layout_width="90dp"
                android:layout_height="37dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="取消"
                android:textColor="#ffffff"
                android:textSize="16sp" />

            <Button
                android:id="@+id/right_button"
                android:layout_width="90dp"
                android:layout_height="37dp"
                android:layout_marginLeft="12dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="确定"
                android:textColor="#ffffff"
                android:textSize="16sp" />
        </LinearLayout>
    </FrameLayout>

</RelativeLayout>

2.使用二

上面是使用默认的样式,下面我们看看如何改变样式
我们把布局文件改成如下样子,


          <com.squareup.timessquare.CalendarPickerView
              android:id="@+id/calendar_picker"
              android:layout_width="wrap_content"
              android:overScrollMode="never"
              android:fadingEdge="none"
              android:scrollbars="none"
              android:layout_height="wrap_content"
              android:background="@color/custom_background"
              android:scrollbarStyle="outsideOverlay"
              android:clipToPadding="false"
              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"
              app:tsquare_headerTextColor="@color/custom_header_text"
              >
          </com.squareup.timessquare.CalendarPickerView>

这样就改变了样式,我们看看效果图
这里写图片描述

这次是不是变得好看多了呢,最后给出项目代码下载地址

项目源码
官方项目源码及demo如下
android-times-square

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值