自定义dialog

显示默认 03:00
第二行 会根据选择的时间显示 什么时候什么时间
继承的底层还是继承的Dialog
show 方法是用来传入时间的 不用管

显示样式
public class ShowSubscribeUpdataDialog extends BaseDialog implements View.OnClickListener {

    private static final String TAG = "ShowSubscribeUpdataDialog";
    private TextView mSubscribeUpdataTime;

    private TextView mSubscribeUpdataday, mSubscribeUpdatamin;

    private String hour = "03";

    private String minute = "00";

    private final List<String> hourList = new ArrayList<>();

    private final List<String> minuteList = new ArrayList<>();
    WheelView subscribeUpdataOk;
    WheelView subscribeUpdataNo;

    public ShowSubscribeUpdataDialog(Context context) {
        super(context);
    }

    @Override
    View addContentView() {
        return LayoutInflater.from(this.getContext()).inflate(R.layout.custom_update_dialog, null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRootW(R.dimen.dp_576);
        tvBaseDialogTitle.setText(R.string.show_subscribe_update);
        tvBaseDialogLeftButton.setText(R.string.pickerview_submit);
        tvBaseDialogRightButton.setText(R.string.dialog_cancel);
        setSelectDate();
        initView();
    }

    /**
     * WheelView 赋值
     */
    private void initView() {
        subscribeUpdataOk = findViewById(R.id.dialog_undata_hour);
        subscribeUpdataNo = findViewById(R.id.dialog_updata_minute);
        mSubscribeUpdataday = findViewById(R.id.subscribe_updata_day); //tian
        mSubscribeUpdatamin = findViewById(R.id.subscribe_updata_min); //fenhzong
        mSubscribeUpdataTime = findViewById(R.id.subscribe_updata_time);//时间

        subscribeUpdataOk.setItemsVisibleCount(3);
        subscribeUpdataOk.setCyclic(true);
        subscribeUpdataOk.setAlphaGradient(true);
        subscribeUpdataOk.setAdapter(new ArrayWheelAdapter<>(hourList));

        subscribeUpdataNo.setItemsVisibleCount(3);
        subscribeUpdataNo.setCyclic(true);
        subscribeUpdataNo.setAlphaGradient(true);
        subscribeUpdataNo.setAdapter(new ArrayWheelAdapter<>(minuteList));
        mSubscribeUpdataTime.setText(getSelectDateStringTime());
        mSubscribeUpdatamin.setText(getHourMinuteTimeStyle());
        subscribeUpdataOk.setOnItemSelectedListener(index -> {
            hour = hourList.get(index);
            if (minute.isEmpty()) return;
            mSubscribeUpdataTime.setText(getSelectDateStringTime());
            mSubscribeUpdatamin.setText(getHourMinuteTimeStyle());
        });
        subscribeUpdataNo.setOnItemSelectedListener(index -> {
            minute = minuteList.get(index);
            if (hour.isEmpty()) return;
            mSubscribeUpdatamin.setText(getHourMinuteTimeStyle());
        });
    }

    /**
     * 时间轮播
     */
    private void setSelectDate() {
        for (int i = 0; i < 34; i++) {
            if (i < 25) {
                hourList.add(addZero(i));
            }
            if (i == 0 || i == 30) {
                minuteList.add(addZero(i));
            }
        }
    }

    /**
     * 日历
     *
     * @return
     */
    public Calendar getSelectCalendarTime() {
        Calendar nowDate = Calendar.getInstance();
        Calendar selectDate = Calendar.getInstance();
        selectDate.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
        selectDate.set(Calendar.MINUTE, Integer.parseInt(minute));
        if (nowDate.after(selectDate)) { //选择时间小于当前时间 则设置为下一天时间
            selectDate.set(Calendar.DAY_OF_YEAR, selectDate.get(Calendar.DAY_OF_YEAR) + 1);
        }
        LogUtil.d(TAG, "select time = " + selectDate.getTimeInMillis());
        return selectDate;
    }

    /**
     * 月 日
     *
     * @return
     */
    public String getSelectDateStringTime() {
        Calendar selectCalendar = getSelectCalendarTime();

        int monty = selectCalendar.get(Calendar.MONTH);
        int day = selectCalendar.get(Calendar.DAY_OF_MONTH);

        return String.format(Locale.getDefault(),
                getContext().getResources().getString(R.string.time1),
                addZero(monty + 1), addZero(day));
    }

    private String getHourMinuteTimeStyle() {
        return getHourMinuteTime() + "分";
    }

    /**
     * 例如10:00
     *
     * @return
     */
    public String getHourMinuteTime() {
        Calendar selectCalendar = getSelectCalendarTime();
        int hour = selectCalendar.get(Calendar.HOUR_OF_DAY);
        int minute = selectCalendar.get(Calendar.MINUTE);
        mSubscribeUpdataday.setText(TimeUtil.timeFrame(hour));
        return String.format(Locale.getDefault(),
                getContext().getResources().getString(R.string.time2), addZero(hour), addZero(minute));
    }


    private String addZero(int number) {
        return number < 10 ? "0".concat(String.valueOf(number)) : String.valueOf(number);
    }

    public void show(String time) {
        super.show();
        if (time != null && time.contains(":")) {
            String[] sp = time.split(":");
            hour = sp[0];
            minute = sp[1];
        }
        subscribeUpdataOk.setCurrentItem(Integer.parseInt(hour));
        subscribeUpdataNo.setCurrentItem(Integer.parseInt(minute));
    }
}

------------------------------------------------------------------------------------------------------------------

原始写法:

public class ShowSubscribeUpdataDialog extends AlertDialog implements View.OnClickListener{

    private static final String TAG = "ShowSubscribeUpdataDialog";
    
    private TextView mSubscribeUpdataTime;

    private TextView mSubscribeUpdataday, mSubscribeUpdatamin;

    private Button mButtonOk;

    private Button mButtonNo;

    private String buttonOk,buttonNo;

    private String hour = "";

    private String minute = "";

    private View.OnClickListener cancelListener, confirmListener;

    private final List<String> hourList = new ArrayList<>();

    private final List<String> minuteList = new ArrayList<>();

    public ShowSubscribeUpdataDialog(Context context) {
        super(context);
    }

    public ShowSubscribeUpdataDialog setButtonOk(String buttonOk,View.OnClickListener listener) {
        this.buttonOk = buttonOk;
        this.confirmListener = listener;
        return this;
    }

    public ShowSubscribeUpdataDialog setButtonNo(String buttonNo,View.OnClickListener listener) {
        this.buttonNo = buttonNo;
        this.cancelListener = listener;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_update_dialog);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCancelable(false);


        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int) ((size.x)*0.7);//设置为屏幕的0.7倍宽度
        getWindow().setAttributes(p);

        mSubscribeUpdataday = findViewById(R.id.subscribe_updata_day); //tian
        mSubscribeUpdatamin = findViewById(R.id.subscribe_updata_min); //fenhzong
        mSubscribeUpdataTime = findViewById(R.id.subscribe_updata_time);//时间
        mButtonOk = findViewById(R.id.button_updata_left);
        mButtonNo = findViewById(R.id.button_updata_right);

        if (!TextUtils.isEmpty(buttonOk)) {
            mButtonOk.setText(buttonOk);
        }
        if (!TextUtils.isEmpty(buttonNo)) {
            mButtonNo.setText(buttonNo);
        }

        mButtonOk.setOnClickListener(this);
        mButtonNo.setOnClickListener(this);

        setSelectDate();
         initView();
    }

    /**
     * WheelView 赋值
     */
    private void initView() {
        WheelView subscribeUpdataOk = findViewById(R.id.dialog_undata_hour);
        WheelView subscribeUpdataNo = findViewById(R.id.dialog_updata_minute);

        subscribeUpdataOk.setItemsVisibleCount(3);
        subscribeUpdataOk.setCyclic(true);
        subscribeUpdataOk.setAlphaGradient(true);
        subscribeUpdataOk.setAdapter(new ArrayWheelAdapter<>(hourList));

        subscribeUpdataNo.setItemsVisibleCount(3);
        subscribeUpdataNo.setCyclic(true);
        subscribeUpdataNo.setAlphaGradient(true);
        subscribeUpdataNo.setAdapter(new ArrayWheelAdapter<>(minuteList));

        int hours = 3;
        int minutes = 0;
        subscribeUpdataOk.setCurrentItem(hours);
        subscribeUpdataNo.setCurrentItem(minutes);
        hour = addZero(hours);
        minute = addZero(minutes);
        mSubscribeUpdataTime.setText(getSelectDateStringTime());
        mSubscribeUpdatamin.setText(getHourMinuteTime());
        subscribeUpdataOk.setOnItemSelectedListener(index -> {
            hour = hourList.get(index);
            if (minute.isEmpty()) return;
            mSubscribeUpdataTime.setText(getSelectDateStringTime());
            mSubscribeUpdatamin.setText(getHourMinuteTime());
        });
        subscribeUpdataNo.setOnItemSelectedListener(index -> {
            minute = minuteList.get(index);
            if (hour.isEmpty()) return;
            mSubscribeUpdataTime.setText(getSelectDateStringTime());
            mSubscribeUpdatamin.setText(getHourMinuteTime());
        });
    }

    /**
     * 时间轮播
     */
    private void setSelectDate() {
        for (int i = 0; i < 34; i++) {
            if (i < 25) {
                hourList.add(addZero(i));
            }
            if (i == 0|| i==30){
                minuteList.add(addZero(i));
            }
        }
    }

    /**
     * 日历
     * @return
     */
    public Calendar getSelectCalendarTime() {
        Calendar nowDate = Calendar.getInstance();
        Calendar selectDate = Calendar.getInstance();
        selectDate.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
        selectDate.set(Calendar.MINUTE, Integer.parseInt(minute));
        if (nowDate.after(selectDate)) { //选择时间小于当前时间 则设置为下一天时间
            selectDate.set(Calendar.DAY_OF_YEAR, selectDate.get(Calendar.DAY_OF_YEAR) + 1);
        }
        LogUtil.d(TAG, "select time = " + selectDate.getTimeInMillis());
        return selectDate;
    }

    /**
     *  月 日
     * @return
     */
    public String getSelectDateStringTime() {
        Calendar selectCalendar = getSelectCalendarTime();

        int monty = selectCalendar.get(Calendar.MONTH);
        int day = selectCalendar.get(Calendar.DAY_OF_MONTH);

        return String.format(Locale.getDefault(),
                getContext().getResources().getString(R.string.time1),
                addZero(monty + 1), addZero(day));
    }

    /**
     * 时 分
     * @return
     */
    public String getHourMinuteTime() {
        Calendar selectCalendar = getSelectCalendarTime();
        int hour = selectCalendar.get(Calendar.HOUR_OF_DAY);
        int minute = selectCalendar.get(Calendar.MINUTE);
        if (hour <= 7) {
            mSubscribeUpdataday.setText("凌晨");
        }else
        if (hour >= 7 && hour <= 12) {
            mSubscribeUpdataday.setText("早上");
        }else
        if (hour > 12 && hour < 19) {
            mSubscribeUpdataday.setText("下午");
        }else
        if (hour >= 19 && hour <= 24) {
            mSubscribeUpdataday.setText("晚上");
        }
     return String.format(Locale.getDefault(),
             getContext().getResources().getString(R.string.time2), addZero(hour), addZero(minute));
    }

    private String addZero(int number) {
        return number < 10 ? "0".concat(String.valueOf(number)) : String.valueOf(number);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button_updata_left:
                if(confirmListener != null){
                    confirmListener.onClick(view);
                }
                break;
            case R.id.button_updata_right:
                if(cancelListener != null){
                    cancelListener.onClick(view);
                }
                break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值