Android TimePickerDialog - 只有一半的时间(字面意思)

时间:2013-01-14 00:13:05

标签: java android time datepicker timepicker

Mac OS-X

带有ADT的ECLIPSE

机器人

................

问题:当我选择时间时,例如06:00,它将时间设置为18:00,当我选择时间18:00时,它将时间设置为06:00。我似乎无法弄清楚为什么。

................

LOGIC

获取当前日期实例。

OnClickListener设置为ButtonImage。

OnClick调用带有2个参数的showTimeDialog(TextView,当前日期实例)。

TimePicker对话框在设定时间09:00显示。

TimeSet设置Calendar 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;
}

1 个答案:

答案 0 :(得分:0)

<强> SOLUTION:

我的onTimeSetListener onTimeSet()方法使用了HOUR而不是HOUR_OF_DAY。

好的,所以我多年来一直坚持这个,但终于弄明白了。但是因为我对Android和日历很陌生,所以区别于我。

Calendar.HOUR是17:00的5,因为它是PM的第5个小时。 Calendar.HOUR_OF_DAY 17:00返回17。所以这是正确的代码:

private OnTimeSetListener timeSetListener = new OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        activeTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
        activeTime.set(Calendar.MINUTE, minute);
        updateTimeDisplay(activeTimeDisplay, activeTime);
        unregisterTimeDisplay();
    }
};