time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar scal = Calendar.getInstance();
Calendar c = Calendar.getInstance();
timePickerDialog = new TimePickerDialog(Newgame.this, R.style.DialogTheme, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int selectedhour, int selectedminute) {
Calendar datetime = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, selectedhour);
calendar.set(Calendar.MINUTE, selectedminute);
if (calendar.before(GregorianCalendar.getInstance())) {
time.setText("");
Toast.makeText(Newgame.this, "Can't select past time", Toast.LENGTH_SHORT).show();
} else {
Calendar currenttime = Calendar.getInstance();
currenttime.set(Calendar.HOUR_OF_DAY, selectedhour);
currenttime.set(Calendar.MINUTE, selectedminute);
String timeSet = "";
if (selectedhour > 12) {
selectedhour -= 12;
timeSet = "PM";
} else if (selectedhour == 0) {
selectedhour += 12;
timeSet = "AM";
} else if (selectedhour == 12) {
timeSet = "PM";
} else {
timeSet = "AM";
}
String min = "";
if (selectedminute < 10)
min = "0" + selectedminute;
else
min = String.valueOf(selectedminute);
String aTime = new StringBuilder().append(selectedhour).append(':').append(min).append(" ").append(timeSet).toString();
time.setText(aTime);
}
}
},scal.get(Calendar.HOUR_OF_DAY),scal.get(Calendar.MINUTE), false);
timePickerDialog.setTitle("Select Time");
timePickerDialog.updateTime(c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE));
timePickerDialog.show();
}
});
当我们单击时间时,它不应该选择今天的过去时间,而应该可以选择将来的时间。在接下来的几天里,时间应该是从12:00 AM到11:59Pm,但是我的代码无法占用今天的过去时间。
例如,现在是06:00 PM,我的应用应该可以在06:00 PM之后花费时间,而今天不应该在06:00 PM之前花费时间,但是在接下来的几天包括明天在内,应该可以在下午06:00之前和之后花费时间。