这是我的代码,我想在timepicker dilog中限制未来的时间。
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
// TODO Auto-generated method stub
hour = hourOfDay;
minute = minutes;
updateTime(hour, minute);
}
};
private static String utilTime(int value) {
if (value < 10)
return "0" + String.valueOf(value);
else
return String.valueOf(value);
}
// Used to convert 24hr format to 12hr format with AM/PM values
private void updateTime(int hours, int mins) {
String timeSet = "";
if (hours > 12) {
hours -= 12;
timeSet = "PM";
} else if (hours == 0) {
hours += 12;
timeSet = "AM";
} else if (hours == 12)
timeSet = "PM";
else
timeSet = "AM";
String minutes = "";
if (mins < 10)
minutes = "0" + mins;
else
minutes = String.valueOf(mins);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hours).append(':')
.append(minutes).append(" ").append(timeSet).toString();
txttime.setText(aTime);
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
Date date = null;
try {
date = parseFormat.parse(aTime.toString());
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(parseFormat.format(date) + " = " + displayFormat.format(date));
Log.e("date :", "date is " + displayFormat.format(date));
timeconvert = displayFormat.format(date).toString();
Log.e("timeconvert :", "time is " + timeconvert.toString());
}**strong text**
答案 0 :(得分:1)
最适合您的方法是使用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
);
Calendar now = Calendar.getInstance();
Timepoint mTimepoint = new Timepoint( now.get(Calendar.HOUR),now.get(Calendar.MINUTE),now.get(Calendar.SECOND));
tpd.setMaxTime(mTimepoint);
tpd.show(getFragmentManager(), "TimePickerDialog");
将Material DateTime Picker库添加到项目的最简单方法是将其作为依赖项添加到build.gradle
dependencies {
compile 'com.wdullaer:materialdatetimepicker:3.0.0'
}
答案 1 :(得分:0)
Calendar now = Calendar.getInstance();
timePickerDialog.setMaxTime(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE),now.get(Calendar.SECOND));}