java android:TimePickerDialog 阻止用户选择过去的时间,并且可以选择具有新日期的未来时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41506715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:55:56  来源:igfitidea点击:

android: TimePickerDialog prevent user select past time and can select future time with new date

javaandroidandroid-studioandroid-fragmentsandroid-timepicker

提问by Atif Amin

I am using this link Android TimePickerDialog set max time.

我正在使用此链接Android TimePickerDialog set max time

I am new in android. With the help of this code, I cannot select past time but we cannot select future time. When 12 is selected in timepickerdialog mode change to am automatically according to the next day not past day.

我是安卓新手。在这段代码的帮助下,我无法选择过去的时间,但我们无法选择未来的时间。在 timepickerdialog 模式中选择 12 时,根据第二天而不是过去一天自动更改为 am。

回答by rafsanahmad007

try this code:

试试这个代码:

TimePickerFragment timePickerFragment = new TimePickerFragment();
            timePickerFragment.setOnTimeSetListener(new OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    Calendar datetime = Calendar.getInstance();
                    Calendar c = Calendar.getInstance();
                    datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    datetime.set(Calendar.MINUTE, minute);
                    if (datetime.getTimeInMillis() >= c.getTimeInMillis()) {
                        //it's after current
                         int hour = hourOfDay % 12;
                    btnPickStartTime.setText(String.format("%02d:%02d %s", hour == 0 ? 12 : hour,
                            minute, hourOfDay < 12 ? "am" : "pm"));
                    } else {
                        //it's before current'
                        Toast.makeText(getApplicationContext(), "Invalid Time", Toast.LENGTH_LONG).show();
                    }
                }
            });
            timePickerFragment.show(getSupportFragmentManager(), "TIME");

it will show the timepickerdialog and if user select any time before current time... it will show invalid time..

它将显示timepicker对话框,如果用户选择当前时间之前的任何时间...它将显示无效时间..

EDITYou need to import:

编辑您需要导入:

import android.app.TimePickerDialog;
import android.app.Fragment;
import android.app.DialogFragment;
import java.util.Calendar;
import android.widget.TimePicker;

Here is a Good Example

这是一个很好的例子

回答by Dmitry Velychko

The best solution for you will be to use thismaterial design library to pick time or date and set min value.

对您来说最好的解决方案是使用这个材料设计库来选择时间或日期并设置最小值。

    TimePickerDialog tpd = TimePickerDialog.newInstance(
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
                //put some interesting code
            }
        },
        now.get(Calendar.HOUR_OF_DAY),
        now.get(Calendar.MINUTE),
        true
    );
    tpd.setMinTime(10, 0, 0); // MIN: hours, minute, secconds
    tpd.show(getFragmentManager(), "TimePickerDialog");

The easiest way to add the Material DateTime Picker library to your project is by adding it as a dependency to your build.gradle

将 Material DateTime Picker 库添加到您的项目的最简单方法是将其作为依赖项添加到您的 build.gradle

dependencies {
    compile 'com.wdullaer:materialdatetimepicker:3.0.0'
}

Hope this will help you.

希望这会帮助你。