Mac OS-X
带 ADT 的 ECLIPSE
安卓
......
问题:当我选择时间时,比如 06:00,它会将时间设置为 18:00,当我选择时间 18:00 时,它会将时间设置为 06:00。我似乎无法弄清楚为什么。
......
逻辑
获取当前日期实例。
OnClickListener 设置为 ButtonImage。
OnClick 使用 2 个参数(一个 TextView,当前日期实例)调用 showTimeDialog。
TimePicker 对话框在 09:00 的设定时间显示。
TimeSet 设置日历 activeTime 的时间并更新 TextView
.....................
其他新信息
好的,发生了这么奇怪的事情,现在我的代码似乎可以工作,但上次我检查它是在 24:00 之前,它的行为与描述的一样。它现在可以工作,英国的时间是 00:20。我不知道这是否与它有关,但如果有任何变化,我会更新这篇文章。
附加新信息 2
我的代码似乎在 00:00 - 12:00 之间工作,但从 12:00 - 24:00 却不行。我难住了。。
......
这是我的代码:
/////// SETUP TIME
// capture our View elements for the start time function
startTimeDisplay = (TextView) findViewById(R.id.editText2);
endTimeDisplay = (TextView) findViewById(R.id.editText3);
startPickTime = (ImageButton) findViewById(R.id.imageButtonStartTime);
endPickTime = (ImageButton) findViewById(R.id.imageButtonEndTime);
// Use the current time as the default values for the time picker
startTime = Calendar.getInstance();
endTime = Calendar.getInstance();
startTime.get(Calendar.DATE);
endTime.get(Calendar.DATE);
// add a click listener to the button
startPickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showTimeDialog(startTimeDisplay, startTime);
}
});
/* add a click listener to the button */
endPickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showTimeDialog(endTimeDisplay, endTime);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
getTime(activeTime);
int hr = activeTime.get(Calendar.HOUR_OF_DAY);
int mn = activeTime.get(Calendar.MINUTE);
Log.d(TAG, "first hr..." +hr);
Log.d(TAG, "first mn..." +mn);
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timeSetListener, THE_HOUR, THE_MINUTE, true);
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
getTime(activeTime);
int hr = activeTime.get(Calendar.HOUR_OF_DAY);
int mn = activeTime.get(Calendar.MINUTE);
Log.d(TAG, "second hr..." +hr);
Log.d(TAG, "second mn..." +mn);
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
break;
case TIME_DIALOG_ID:
((TimePickerDialog) dialog).updateTime(THE_HOUR, THE_MINUTE);
break;
}
}
public void showTimeDialog(TextView timeDisplay, Calendar date) {
activeTimeDisplay = timeDisplay;
activeTime = date;
showDialog(TIME_DIALOG_ID);
}
private OnTimeSetListener timeSetListener = new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
activeTime.set(Calendar.HOUR, hour);
activeTime.set(Calendar.MINUTE, minute);
updateTimeDisplay(activeTimeDisplay, activeTime);
unregisterTimeDisplay();
}
};
private void updateTimeDisplay(TextView timeDisplay, Calendar date) {
//convert Calendar to Unix Time
long activeTimeUnixTime = (long) (date.getTimeInMillis());
//convert Unix Time to Date
java.util.Date startTimeDate = new java.util.Date(activeTimeUnixTime);
//convert Date to SimpleDateFormat and convert to String
SimpleDateFormat formatter2;
formatter2 = new SimpleDateFormat("kk:mm", Locale.UK);
String stringTime = formatter2.format(startTimeDate);
timeDisplay.setText(stringTime);
}
private void unregisterTimeDisplay() {
activeTimeDisplay = null;
activeTime = null;
}