2012-12-01 36 views
5

例如,在18:00創建此timePicker(下圖)時,timePicker的timeValue將爲06:00。在19:44-> 07:44 ...因此,從AM/PM切換到24h模式後,它不會移動到當前正確的小時。我該如何改變它? 我的目標是timePicker顯示創建時的當前時間。就像我在AM/PM模式下使用它一樣。切換到24小時後,TimePicker小時不會更新

TimePicker timePicker = (TimePicker) convertView.findViewById(R.id.time_picker); 
timePicker.setIs24HourView(true); 

Sry爲糟糕的表述,希望你能理解我的問題。否則就問。

UPDATE:

public class ChooseTimeViewDialog extends AlertDialog { 

    protected Context context; 


    public ChooseTimeViewDialog(final Context context) { 
     super(context);  
     this.context = context; 
    } 

    public void onCreate(Bundle savedInstanceState) { 
     LayoutInflater inflater = this.getLayoutInflater(); 

     View convertView = inflater.inflate(R.layout.dialog_choose_time_view, null); 
     setContentView(convertView); 
     setTitle(R.string.title_dialog_choose_time_view); 

     final TimePicker taskTimePicker = (TimePicker) convertView.findViewById(R.id.dctv_task_time_picker); 
     taskTimePicker.setIs24HourView(true); 


     setButton(DialogInterface.BUTTON_POSITIVE, "Choose", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       int hour = taskTimePicker.getCurrentHour(); 
       int minute = taskTimePicker.getCurrentMinute(); 

       Calendar cal = new GregorianCalendar(); 
       cal.set(0, 0, 0, hour, minute, 0); 

       ((AddTaskViewActivity) context).setSelectedTime(cal.getTime()); 
      } 
     }); 
     setButton(DialogInterface.BUTTON_NEUTRAL, "No time", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       ((AddTaskViewActivity) context).setSelectedTime(null); 
      } 
     }); 

    } 

} 

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TimePicker 
     android:id="@+id/dctv_task_time_picker" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

後,我搬到了initialiation到的onCreate和使用的setContentView(查看),在對話框的背景是透明的,它是全屏,並沒有buttons..why?

+0

嗯,我可以修復它,但它不是一個真正的很好的解決方案,更好嗎? timePicker.setCurrentHour(new Date()。getHours()); – StupidBird

+0

這段代碼何時執行?很多小部件在創建後都不會重新繪製。 – rds

+0

它在一個名爲ChooseTimeDialog的自定義AlertDialog的構造函數中執行。每次打開對話框時,我都會創建一個新的對象:new ChooseTimeDialog(this).show(); – StupidBird

回答

7

我可以用timePicker.setCurrentHour初始化對話框(新的Date()。調用getHours()),但是這不是我想要的解決方案。應該有可能以更好的方式解決它。

望着source code,這是後出現的某個錯誤API 10:

public void setIs24HourView(Boolean is24HourView) { 
    if (mIs24HourView == is24HourView) { 
     return; 
    } 
    mIs24HourView = is24HourView; 
    // cache the current hour since spinner range changes 
    int currentHour = getCurrentHour(); 
    updateHourControl(); 
    // set value after spinner range is updated 
    setCurrentHour(currentHour); 
    updateAmPmControl(); 
} 

正確的順序是:

// cache the current hour since spinner range changes 
int currentHour = getCurrentHour(); 
mIs24HourView = is24HourView; 

因爲getCurrentHour()依靠mIs24HourView返回1- 12或0-23 ...

但是,直到源代碼更新和deplo yed,你沒有太多的選擇。您需要自行撥打setIs24HourView()後致電setCurrentHour()

0

你應該onCreate

+1

當我在onCreate中初始化並使用setContentView(view)時,對話框具有透明背景,填充整個屏幕並且沒有按鈕....我會在我的問題中發佈整個代碼,也許有人發現錯誤 – StupidBird

+0

我很抱歉,但是你可能知道爲什麼當我使用onCreate而不是構造函數時它工作正常嗎?這讓我瘋狂!似乎所有的setButton(),setTitle()方法都被忽略... – StupidBird

0

除了公認的答案,如果你正在使用Xamarin,那麼你可以通過繼承TimePicker和壓倒一切的SetIs24HourView像這樣做同樣的事情:

public class TwentyFourHourMvxTimePicker : MvxTimePicker 
{ 
    public TwentyFourHourMvxTimePicker(Context context, IAttributeSet attrs) : base (context, attrs) 
    { 

    } 

    public override void SetIs24HourView (Java.Lang.Boolean is24HourView) 
    { 
     var current = CurrentHour; 
     base.SetIs24HourView(is24HourView); 
     CurrentHour = current; 
    } 
}