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

cwdobuhd  于 2022-11-14  发布在  Android
关注(0)|答案(3)|浏览(121)

我正在使用此链接Android TimePickerDialog设置最大时间。
我是android的新手。在这个代码的帮助下,我不能选择过去的时间,但我们不能选择未来的时间。当在timepickerdialog模式中选择12时,会自动根据第二天而不是过去的一天更改为上午。

ttygqcqt

ttygqcqt1#

请尝试以下代码:

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");

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

编辑您需要导入:

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

这里是一个好的Example

prdp8dxp

prdp8dxp2#

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

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");

将Material DateTime Picker库添加到项目的最简单方法是将其作为依赖项添加到构建版本。gradle

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

46qrfjad3#

private int  mHour, mMinute;
final Calendar c = Calendar.getInstance();

mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(ActivityScheduleTransfer.this,
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                final Calendar checkOldDate = Calendar.getInstance();
                  if (hourOfDay >= checkOldDate.get(Calendar.HOUR_OF_DAY)) { 
                      if (hourOfDay == checkOldDate.get(Calendar.HOUR_OF_DAY) && minute <= checkOldDate.get(Calendar.MINUTE)) {
                                return;
                        }
                    //select current after 
                } else {`enter code here`
                    //select current before 
                }
            }
        }, mHour, mMinute, false);
            timePickerDialog.show();

相关问题