浏览代码

日期帅选

luanxuye 5 年之前
父节点
当前提交
96f6fc5623

+ 99 - 0
base_library/src/main/java/com/mgtech/base_library/util/DateSelectDialogUtil.java

@@ -0,0 +1,99 @@
+package com.mgtech.base_library.util;
+
+import android.content.Context;
+import android.view.View;
+import android.widget.TextView;
+
+import com.bigkoo.pickerview.builder.TimePickerBuilder;
+import com.bigkoo.pickerview.listener.CustomListener;
+import com.bigkoo.pickerview.listener.OnTimeSelectListener;
+import com.bigkoo.pickerview.view.TimePickerView;
+import com.mgtech.base_library.R;
+
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * @ProjectName: ShellApplication
+ * @Package: com.tennor.base_library.util
+ * @ClassName: DateSelectDialogUtil
+ * @Description: 时间选择器
+ * @Author: 牛松涛
+ * @CreateDate: 2020/5/12 13:16
+ * @UpdateUser: 更新者:
+ * @UpdateDate: 2020/5/12 13:16
+ * @UpdateRemark: 更新说明:
+ * @Version: 1.0
+ */
+public class DateSelectDialogUtil {
+    private Context context;
+    private TimePickerView timePickerView;
+    private Calendar startDate,endDate;
+    private ChooseDateListener chooseDateListener;
+
+
+    public DateSelectDialogUtil(Context context, ChooseDateListener chooseDateListener) {
+        this.context = context;
+        this.chooseDateListener = chooseDateListener;
+    }
+
+    private void initCalendar(Date startDateString,Date endDateString){
+        if (startDate == null)
+            startDate = Calendar.getInstance();
+//        startDate.set(2013, 0, 23);
+        startDate.setTime(startDateString);
+        if (endDate == null)
+            endDate = Calendar.getInstance();
+//        endDate.set(2019, 11, 28);
+        endDate.setTime(endDateString);
+        initTimePicker();
+    }
+
+    private void initTimePicker() {//Dialog 模式下,在底部弹出
+        Calendar selectedDate = Calendar.getInstance();
+        timePickerView = new TimePickerBuilder(context, new OnTimeSelectListener() {
+            @Override
+            public void onTimeSelect(Date date, View v) {
+                if (chooseDateListener != null)
+                    chooseDateListener.chooseDate(DateFormatUtil.getYmdString(date));
+            }
+        }).setLayoutRes(R.layout.layout_pickerview_custom_time, new CustomListener() {
+
+                    @Override
+                    public void customLayout(View v) {
+                        TextView tvSubmit = v.findViewById(R.id.picker_confirm);
+                        TextView ivCancel = v.findViewById(R.id.picker_cancel);
+                        tvSubmit.setOnClickListener(new View.OnClickListener() {
+                            @Override
+                            public void onClick(View v) {
+                                timePickerView.returnData();
+                                dialogDismiss();
+                            }
+                        });
+                        ivCancel.setOnClickListener(v1 -> dialogDismiss());
+                    }
+                })
+                .setType(new boolean[]{true, true, true, false, false, false})
+                .setLabel("", "", "", "", "", "")
+                .setDate(selectedDate)
+                .setRangDate(startDate, selectedDate)
+                .setLineSpacingMultiplier(2.0f)
+                .build();
+    }
+
+    public void dialogShow(Date startDateString,Date endDateString){
+        initCalendar(startDateString, endDateString);
+        if (timePickerView != null && !timePickerView.isShowing())
+            timePickerView.show();
+    }
+
+    public void dialogDismiss(){
+        if (timePickerView != null && timePickerView.isShowing())
+            timePickerView.dismiss();
+    }
+
+
+    public interface ChooseDateListener{
+        void chooseDate(String date);
+    }
+}

+ 74 - 0
base_library/src/main/res/layout/layout_pickerview_custom_time.xml

@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:background="@color/white"
+    android:layout_height="match_parent">
+
+    <TextView
+        android:id="@+id/picker_cancel"
+        style="@style/black_text_normal"
+        android:padding="@dimen/dp_15"
+        android:text="@string/common_cancel"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
+
+    <TextView
+        android:id="@+id/picker_confirm"
+        style="@style/black_text_normal"
+        android:padding="@dimen/dp_15"
+        android:text="@string/common_confirm"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <LinearLayout
+        android:id="@+id/timepicker"
+        android:layout_width="match_parent"
+        android:layout_height="@dimen/dp_0"
+        android:background="@android:color/white"
+        android:gravity="center"
+        android:minHeight="@dimen/dp_200"
+        android:paddingBottom="@dimen/dp_50"
+        android:orientation="horizontal"
+        app:layout_constraintTop_toBottomOf="@+id/picker_confirm">
+
+        <com.contrarywind.view.WheelView
+            android:id="@+id/year"
+            android:layout_width="0dp"
+            android:layout_height="match_parent"
+            android:layout_weight="1" />
+
+        <com.contrarywind.view.WheelView
+
+            android:id="@+id/month"
+            android:layout_width="0dp"
+            android:layout_height="match_parent"
+            android:layout_weight="1.1" />
+
+        <com.contrarywind.view.WheelView
+            android:id="@+id/day"
+            android:layout_width="0dp"
+            android:layout_height="match_parent"
+            android:layout_weight="1.1" />
+
+        <com.contrarywind.view.WheelView
+            android:id="@+id/hour"
+            android:layout_width="0dp"
+            android:layout_height="match_parent"
+            android:layout_weight="1.1" />
+
+        <com.contrarywind.view.WheelView
+            android:id="@+id/min"
+            android:layout_width="0dp"
+            android:layout_height="match_parent"
+            android:layout_weight="1.1" />
+
+        <com.contrarywind.view.WheelView
+            android:id="@+id/second"
+            android:layout_width="0dp"
+            android:layout_height="match_parent"
+            android:layout_weight="1.1" />
+    </LinearLayout>
+
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 1 - 0
base_library/src/main/res/values/colors.xml

@@ -52,6 +52,7 @@
     <color name="login_btn_end">#0095F4</color>
     <color name="gray_f8">#f8f8f8</color>
     <color name="gray_f6">#f6f6f6</color>
+    <color name="gray">#D8D8D8</color>
     <color name="all_yellow">#FFD700</color>
     <color name="f2f2f2">#F2F2F2</color>
     <color name="_787878">#787878</color>

+ 6 - 1
base_library/src/main/res/values/styles.xml

@@ -46,7 +46,12 @@
         <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
         <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
     </style>
-
+    <style name="black_text_normal">
+        <item name="android:layout_width">wrap_content</item>
+        <item name="android:layout_height">wrap_content</item>
+        <item name="android:textColor">@color/black_text</item>
+        <item name="android:textSize">@dimen/sp_14</item>
+    </style>
 
     <!--弹窗效果-->
     <style name="MyDialogStyle" parent="Theme.AppCompat.Light.NoActionBar">