Android 时间选择器小时不';t切换到24小时后更新

Android 时间选择器小时不';t切换到24小时后更新,android,time,timepicker,Android,Time,Timepicker,例如,当我在18:00创建此时间选择器(如下)时,时间选择器的时间值将为06:00。在19:44->07:44。。。因此,从AM/PM切换到24小时模式后,它不会移动到正确的当前小时数。我怎样才能改变这一点? 我的目标是时间选择器显示创建它时的当前时间。就像我在AM/PM模式下使用它一样 TimePicker timePicker = (TimePicker) convertView.findViewById(R.id.time_picker); timePicker.setIs24HourVi

例如,当我在18:00创建此时间选择器(如下)时,时间选择器的时间值将为06:00。在19:44->07:44。。。因此,从AM/PM切换到24小时模式后,它不会移动到正确的当前小时数。我怎样才能改变这一点? 我的目标是时间选择器显示创建它时的当前时间。就像我在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:



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

您应该在

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

看看,这是API 10之后出现的一个bug:

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


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

除了接受的答案之外,如果您使用的是Xamarin,那么您可以通过子类化时间选择器并覆盖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;
    }
}

嗯,我可以解决它,但这不是一个很好的解决方案,更好吗?timePicker.setCurrentHour(新日期().getHours());这个代码什么时候执行?许多小部件在创建后不会神奇地重新绘制。它是在我创建的名为ChooseTimeDialog的自定义AlertDialog的构造函数中执行的。每次打开对话框时,我都会为它创建一个新对象:new ChooseTimeDialog(this.show()@rds您的评论听起来有点讽刺和困惑,除非您相信像
setCurrentHour()
这样的方法是“神奇的”。为什么在调用
setIs24HourView()
之后不调用
setCurrentHour()
?当我在onCreate中初始化它并使用setContentView(视图)时,对话框有透明的背景,填充整个屏幕,没有按钮。。。。我将在我的问题中发布整个代码,可能有人发现了错误我很抱歉,但是你知道为什么我使用onCreate而不是构造函数时它不能正常工作吗?快把我逼疯了!似乎所有的setButton()、setTitle()方法都被忽略了。。。
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;
    }
}