2013-01-14 15 views
1

的Mac OS-X的Android TimePickerDialog - 僅適用一半的時間(直譯)

使用adt

ANDROID

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

問題:當我選擇時間,比如06:00,它將時間設置爲18:00,當我選擇時間18:00時,它將時間設置爲06:00。我似乎無法弄清楚爲什麼。

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

LOGIC

獲取當前日期的實例。

OnClickListener設置爲ButtonImage。

OnClick使用2個參數(一個TextView,當前日期實例)調用showTimeDialog。

TimePicker對話框在設定的時間09:00顯示。

TimeSet設置日曆activeTime的時間和更新一個TextView

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

追加新的信息

好了,奇怪的事情發生了,現在我的代碼似乎工作,但上次我檢查它是在24:00之前,它的行爲如上所述。它現在有效,現在在英國的時間是00:20。我不知道這是否與它有任何關係,但如果有任何更改,我會更新這篇文章。

額外的新信息2

我的代碼似乎00:00之間的工作 - 它不24:00 - 12:00,但12: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; 
} 

回答

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(); 
    } 
}; 
相關問題