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

Stu*_*ird 5 time android 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);
Run Code Online (Sandbox Code Playgroud)

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

更新:

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);
            }
        });

    }

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

Sam*_*Sam 7

我可以使用timePicker.setCurrentHour(new Date().getHours()),但这不是我想要的解决方案.应该可以以更好的方式解决它.

查看源代码,这是在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();
}
Run Code Online (Sandbox Code Playgroud)

正确的顺序是:

// cache the current hour since spinner range changes
int currentHour = getCurrentHour();
mIs24HourView = is24HourView;
Run Code Online (Sandbox Code Playgroud)

因为getCurrentHour()依赖于mIs24HourView返回1-12或0-23 ......

但是在更新和部署源代码之前,您没有太多选择.你需要调用setCurrentHour()setIs24HourView()自己.