切换到24h后,TimePicker小时不会更新

时间:2012-12-01 18:16:59

标签: android time timepicker

例如,当我在18:00创建此timePicker(下面)时,timePicker的timeValue将是06:00。在19:44-> 07:44 ...因此在从AM / PM切换到24h模式之后它不会移动到正确的当前小时。我怎么能改变呢? 我的目标是timePicker显示创建它的当前时间。就像我在AM / PM模式下使用它时一样。

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

对于糟糕的表述,希望你能理解我的问题。否则就问问。

更新:

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>

在我将初始化移动到onCreate并使用setContentView(View)后,对话框的背景是透明的,它是全屏的,没有按钮..为什么?

3 个答案:

答案 0 :(得分:7)

  

我可以使用timePicker.setCurrentHour(new 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 ...

但是在更新和部署源代码之前,您没有太多选择。您需要在setCurrentHour()之后致电setIs24HourView()

答案 1 :(得分:0)

您应该在onCreate

中初始化对话框

答案 2 :(得分: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;
    }
}