如何在时间选择器对话框中添加额外1小时。 例如: - 如果当前时间是4.00平方米,那么我想在时间选择器中显示下午5点。
final Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR,1);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
timePickerDialog = new TimePickerDialog(CheckOut.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
tvTimeSelect.setText("Delivery Time : "+hourOfDay + ":" + minute);
timePickerDialog.dismiss();
}
}, mHour, mMinute, false);
timePickerDialog.show();
请告诉我解决方案......
答案 0 :(得分:3)
您需要保持一致性。 如果你得到HOUR_OF_DAY,那么你需要设置相同的
选项1 :使用HOUR_OF_DAY
final Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR_OF_DAY,1);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
timePickerDialog = new TimePickerDialog(CheckOut.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
tvTimeSelect.setText("Delivery Time : "+hourOfDay + ":" + minute);
timePickerDialog.dismiss();
}
}, mHour, mMinute, false);
timePickerDialog.show();
选项2 :使用小时
final Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR,1);
mHour = c.get(Calendar.HOUR);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
timePickerDialog = new TimePickerDialog(CheckOut.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
tvTimeSelect.setText("Delivery Time : "+hourOfDay + ":" + minute);
timePickerDialog.dismiss();
}
}, mHour, mMinute, false);
timePickerDialog.show();
答案 1 :(得分:1)
试试这个c.add(Calendar.HOUR_OF_DAY,1);
代替c.add(Calendar.HOUR,1);
答案 2 :(得分:1)
检查一下,测试它的工作
Calendar mcurrentTime = Calendar.getInstance();
mcurrentTime.add(Calendar.HOUR,1);
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
tvTimeSelect.setText(selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
答案 3 :(得分:1)
如果你想要12H而不是24H,你不应该把它改成这个吗?
mHour = c.get(Calendar.HOUR);
http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html