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<String> 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);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章