2012-12-01 58 views
5

Ví dụ: khi tôi tạo timePicker này (bên dưới) lúc 18:00, timePicker của timeValue sẽ là 06:00. Lúc 19: 44-> 07:44 ... Vì vậy, nó không chuyển sang giờ hiện tại chính xác sau khi chuyển từ AM/PM sang chế độ 24h. Làm thế nào tôi có thể thay đổi điều đó? Mục tiêu của tôi là timePicker hiển thị thời gian hiện tại khi nó được tạo. Giống như nó khi tôi sử dụng nó trong chế độ AM/PM.TimePicker giờ không cập nhật sau khi chuyển sang 24h

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

Sry vì công thức xấu, hy vọng bạn có thể hiểu câu hỏi của tôi. Nếu không, chỉ cần hỏi.

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> 

Sau khi tôi chuyển initialiation để onCreate và sử dụng setContentView (View), nền của hộp thoại là minh bạch, đó là toàn màn hình và không có nút..why?

+0

Vâng tôi có thể sửa chữa nó, nhưng nó không phải là thực sự là một giải pháp tốt đẹp, tốt hơn? timePicker.setCurrentHour (new Date(). GetHours()); – StupidBird

+0

Khi nào mã này được thực thi? Nhiều tiện ích sẽ không vẽ lại một cách kỳ diệu sau khi chúng được tạo. – rds

+0

Nó được thực thi trong hàm tạo của một AlertDialog tùy chỉnh được gọi là ChooseTimeDialog mà tôi đã tạo. Mỗi khi tôi mở hộp thoại, tôi tạo một đối tượng mới của nó: new ChooseTimeDialog (this) .show(); – StupidBird

Trả lời

7

tôi có thể sử dụng timePicker.setCurrentHour (new Date(). GetHours()), nhưng đó không phải là giải pháp tôi muốn. Có thể giải quyết nó theo cách tốt hơn.

Nhìn vào source code, đây là một lỗi đã xuất hiện đôi khi sau khi 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(); 
} 

Trình tự đúng là:

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

getCurrentHour() dựa vào mIs24HourView trở 1- 12 hoặc 0-23 ...

Nhưng cho đến khi mã nguồn được cập nhật và deplo yed, bạn không có nhiều sự lựa chọn. Bạn cần phải gọi setCurrentHour() sau setIs24HourView() chính mình.

0

bạn nên khởi thoại của bạn trong onCreate

+1

Khi tôi khởi tạo nó trong onCreate và cũng sử dụng setContentView (xem), hộp thoại có nền trong suốt, lấp đầy toàn màn hình và không có nút ....Tôi sẽ đăng toàn bộ mã trong câu hỏi của tôi, có thể ai đó tìm thấy sai lầm – StupidBird

+0

Tôi xin lỗi, nhưng bạn có biết lý do tại sao nó không hoạt động tốt khi tôi sử dụng onCreate thay vì constructor? Nó khiến tôi điên lên! Có vẻ như tất cả các sethtutton(), setTitle() mehtods đều bị bỏ qua ... – StupidBird

0

Ngoài các câu trả lời được chấp nhận, nếu bạn đang sử dụng Xamarin sau đó bạn có thể làm điều tương tự bởi subclassing TimePicker và SetIs24HourView trọng như vậy:

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; 
    } 
} 
Các vấn đề liên quan