Timepicker对话框的最短时间

时间:2018-01-05 09:35:24

标签: android timepicker

如何在timepicker对话框上设置最小时间或在android中的timepicker对话框中禁用过去时间。 任何图书馆建议?

1 个答案:

答案 0 :(得分:1)

public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener

对于时间选择器

@SuppressLint("WrongConstant")
public void showTimePicker(Calendar calendar) {
    Calendar minTimeCalendar = Calendar.getInstance();
    minTimeCalendar.setTime(localToGMT(minTimeCalendar.getTime()));
    TimePickerDialog tpd = TimePickerDialog.newInstance(this, calendar.get(11), calendar.get(12), true);
    if (compareToDate(calendar.getTime(), minTimeCalendar.getTime())) {
        minTimeCalendar.add(12, 60);
        tpd.setMinTime(minTimeCalendar.get(11), 0, 0);
    }

    **tpd.enableMinutes(false);**
    tpd.setVersion(TimePickerDialog.Version.VERSION_2);
    tpd.show(getFragmentManager(), "TimePickDialog");
}

对于日期选择器

 public void showDatePicker(Calendar calendar) {
    Calendar minDateCalendar = Calendar.getInstance();

    if (currentHour == 24) {
        minDateCalendar.add(Calendar.DAY_OF_YEAR, 1);
    }
    minDateCalendar.setTime(localToGMT(minDateCalendar.getTime()));
    @SuppressLint("WrongConstant") DatePickerDialog dpd = DatePickerDialog.newInstance(this, calendar.get(1), calendar.get(2), calendar.get(5));

    dpd.setMinDate(minDateCalendar);
    dpd.setVersion(DatePickerDialog.Version.VERSION_2);
    dpd.show(getFragmentManager(), "DatePickerDialog");
}

两种覆盖方法

@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
     //display selected date here code
}

@Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
    //display selected time here code
}

希望这有助于您......如果您需要任何帮助,可以提出