设置最大值和最小值

设置最大值和最小值

本文介绍了日期选择器和时间选择器 - 设置最大值和最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作类似提醒应用的东西.我想让用户选择不是现在的日期和时间(至少 5 分钟后),我还想禁止用户选择太远的日期 - 例如 30 天.我创建了 datePicker 和 timePicker,使它们在单击按钮时弹出,但找不到设置最小值和最大值的方法.

I am trying to make something like a reminder app. I want to allow the user to select date and time that is not now (at least 5 minutes from now), and I also want to disable the user from selecting a date that is too far away - 30 days for example. I created datePicker and timePicker, made them pop up on button click, but could not find way to set the min and max values.

 public void showDateDialog() {
 btnDate = (Button) findViewById(R.id.buttonDate);

    btnDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });
}
    public void showTimeDialog() {
    btnTime = (Button) findViewById(R.id.buttonTime);

    btnTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID);
        }
    });
}
@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            // create a new DatePickerDialog with values you want to show
            return new DatePickerDialog(this, datePickerListener, yearSet, monthSet, daySet);
        // create a new TimePickerDialog with values you want to show
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this, timePickerListener, hourSet, minuteSet, false);
    }
    return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        yearSet = year;
        monthSet = monthOfYear + 1;
        daySet = dayOfMonth;
        btnDate.setText(new SimpleDateFormat("DD").format(daySet) + "-" + new SimpleDateFormat("MMM").format(monthSet) + "-" + yearSet);
    }
};
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hour, int minute) {
        hourSet = hour;
        minuteSet = minute;
        btnTime.setText(hourSet + ":" + new SimpleDateFormat("MM").format(hourSet));
    }
};

推荐答案

试试这个:

btnDate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            }
        };

        Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);

        final DatePickerDialog datePickerDialog = new DatePickerDialog(
                Main23Activity.this, datePickerListener,
                mYear, mMonth, mDay);
        DatePicker datePicker = datePickerDialog.getDatePicker();

        c.add(Calendar.MONTH, +1);
        long oneMonthAhead = c.getTimeInMillis();
        datePicker.setMaxDate(oneMonthAhead);
        datePicker.setMinDate(System.currentTimeMillis() - 1000);
        datePickerDialog.show();

    }
});

btnTime.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hour, int minute) {

            }
        };
        Calendar c = Calendar.getInstance();

        final TimePickerDialog timePickerDialog = new TimePickerDialog(Main23Activity.this,timePickerListener,
                c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE)+5,false);
        timePickerDialog.show();

    }
});

您需要检查用户是否选择了今天的日期并且时间小于当前时间条件.

You need to check if the user selects today date and the time is less than the current time condition though.

这篇关于日期选择器和时间选择器 - 设置最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:21