Android 自定义选择器Dialog(单选,时间选择)



import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.ruanmeng.newstar.R;
import com.ruanmeng.newstar.nohttp.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @param
 * @Comments : 选择器
 * @Author : Lampo
 * @CreateDate : 2019/7/6 15:24
 * @ModifiedBy : Lampo
 * @ModifiedDate : 2019/7/6 15:24
 * @Modified :
 */

public class SelectorDialogUtils {
    private static OptionsPickerView optionsPickerView;
    private static TimePickerView timePickerView;
    private static SelectorCallback callback;
    private static SelectorDialogUtils dialog;

    public void setCallback(SelectorCallback callback) {
        this.callback = callback;
    }

    public static SelectorDialogUtils getInstance() {
        if (dialog == null) {
            dialog = new SelectorDialogUtils();
        }
        return dialog;
    }


    /**
     * 单选
     *
     * @param context
     * @param title   选择器标题
     * @param list    选择器内容
     * @return
     */
    public SelectorDialogUtils initRadioPicker(Context context, String title, List list) {

        optionsPickerView = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (callback != null) {
                    callback.returnValue(list.get(options1));
                }
                optionsPickerView.dismiss();
            }
        })
                .setTitleText(title)
                .setDividerColor(context.getResources().getColor(R.color.divider))//设置选择器中的分割线
                .setTextColorCenter(context.getResources().getColor(R.color.text_333)) //设置选中项文字颜色
                .setContentTextSize(16)
                .setSubmitColor(context.getResources().getColor(R.color.theme))//设置确认按钮文字颜色
                .setSubCalSize(16)
                .setCancelColor(context.getResources().getColor(R.color.text_999))//设置取消按钮文字颜色
                .setTextColorOut(context.getResources().getColor(R.color.text_999))//设置未选中文字颜色
                .setLineSpacingMultiplier(2.5f)
                .build();
        optionsPickerView.setPicker(list);//一级选择器
        optionsPickerView.show();
        return this;
    }

    /**
     * 选择时间
     *
     * @param context
     * @param hasDay  是否可以选择天
     * @return
     */
    public SelectorDialogUtils initDatePicker(Context context, boolean hasDay) {
        Calendar startca = Calendar.getInstance();
        startca.set(startca.get(Calendar.YEAR) - 40, 0, 0);

        final Calendar nowcal = Calendar.getInstance();
        nowcal.set(nowcal.get(Calendar.YEAR), nowcal.get(Calendar.MONTH), nowcal.get(Calendar.DAY_OF_MONTH));

        final Calendar endcal = Calendar.getInstance();
        endcal.set(endcal.get(Calendar.YEAR), endcal.get(Calendar.MONTH), endcal.get(Calendar.DAY_OF_MONTH));

        timePickerView = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat format = new SimpleDateFormat(hasDay ? "yyyy-MM-dd" : "yyyy-MM");
                String time = format.format(new Date(date.getTime()));
                if (callback != null) {
                    callback.returnValue(time);
                }
                timePickerView.dismiss();
            }
        })
                .setTitleText("选择时间")
                .setDate(nowcal)
                .setRangDate(startca, endcal)
                .setType(new boolean[]{true, true, hasDay, false, false, false})
                .setDividerColor(context.getResources().getColor(R.color.divider))//设置选择器中的分割线
                .setTextColorCenter(context.getResources().getColor(R.color.text_333)) //设置选中项文字颜色
                .setContentTextSize(16)
                .setSubmitColor(context.getResources().getColor(R.color.theme))
                .setSubCalSize(16)
                .setCancelColor(context.getResources().getColor(R.color.text_999))
                .setTextColorOut(context.getResources().getColor(R.color.text_999))
                .setLineSpacingMultiplier(2.5f)
                .isDialog(true) //默认设置false ,内部实现将DecorView 作为它的父控件。
                .build();

        Dialog mDialog = timePickerView.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            timePickerView.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
        timePickerView.show();
        return this;
    }

    //选择时间

    /**
     * yyyy-MM-dd
     *
     * @param context
     * @param index       设置  1:开始时间
     * @param tvStartTime 开始时间
     * @param tvEndTime   开始时间
     */
    public void initStartEndPicker(Context context, int index, TextView tvStartTime, TextView tvEndTime) {
        initStartEndPicker(context, index, true, tvStartTime, tvEndTime);
    }

    public void initStartEndPicker(Context context, int index, boolean hasDay, TextView tvStartTime, TextView tvEndTime) {
        Calendar startca = Calendar.getInstance();
        startca.set(startca.get(Calendar.YEAR) - 40, 0, 0);

        final Calendar nowcal = Calendar.getInstance();
        nowcal.set(nowcal.get(Calendar.YEAR), nowcal.get(Calendar.MONTH), nowcal.get(Calendar.DAY_OF_MONTH));

        final Calendar endcal = Calendar.getInstance();
        endcal.set(endcal.get(Calendar.YEAR), endcal.get(Calendar.MONTH), endcal.get(Calendar.DAY_OF_MONTH));

        timePickerView = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {

                SimpleDateFormat format = new SimpleDateFormat(hasDay ? "yyyy-MM-dd" : "yyyy-MM");
                String time = format.format(new Date(date.getTime()));
                if (index == 1) {
                    String a = tvEndTime.getText().toString().trim();
                    if (!TextUtils.isEmpty(a)) {
                        int b = Integer.parseInt(a.replace("-", ""));
                        int b1 = Integer.parseInt(time.replace("-", ""));
                        if (b1 < b) {
                            tvStartTime.setText(time);
                        } else {
                            ToastUtil.showToast(context, "开始时间不能大于结束时间~");
                        }
                    } else {
                        tvStartTime.setText(time);
                    }
                } else {
                    String a = tvStartTime.getText().toString().trim();
                    if (!TextUtils.isEmpty(a)) {
                        int b = Integer.parseInt(a.replace("-", ""));
                        int b1 = Integer.parseInt(time.replace("-", ""));
                        if (b1 > b) {
                            tvEndTime.setText(time);

                        } else {
                            ToastUtil.showToast(context, "结束时间不能小于开始时间~");
                        }
                    } else {
                        tvEndTime.setText(time);
                    }
                }
                timePickerView.dismiss();
            }
        })
                .setTitleText("选择时间")
                .setDate(nowcal)
                .setRangDate(startca, endcal)
                .setType(new boolean[]{true, true, hasDay, false, false, false})
                .setDividerColor(context.getResources().getColor(R.color.divider))
                .setTextColorCenter(context.getResources().getColor(R.color.text_333)) //设置选中项文字颜色
                .setContentTextSize(16)
                .setSubmitColor(context.getResources().getColor(R.color.theme))
                .setSubCalSize(16)
                .setCancelColor(context.getResources().getColor(R.color.text_999))
                .setTextColorOut(context.getResources().getColor(R.color.text_999))
                .setLineSpacingMultiplier(2.5f)
                .isDialog(true) //默认设置false ,内部实现将DecorView 作为它的父控件。
                .build();

        Dialog mDialog = timePickerView.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            timePickerView.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
        timePickerView.show();
    }

    public interface SelectorCallback {
        void returnValue(String value);
    }
}

 

你可能感兴趣的:(UI)