有没有办法以编程方式将 TimePickerDialog 中的 am 更改为 pm?

发布于 2024-10-20 14:29:29 字数 1063 浏览 5 评论 0原文

我正在寻找一种方法,在使用 12 小时格式的同时,在 Android 应用程序的 TimePickerDialog 中动态更改 AM/PM。我希望当对话框中的时间从 11 点更改为 12 点但在用户单击设置按钮之前,将 AM 切换为 PM,反之亦然。这是可能的还是我需要考虑创建自定义 TimePickerDialog 控件?我当前的代码如下。感谢您的帮助!

    // Start Time
    starttime = (Button)findViewById(R.id.starttime); 

    starttime.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) { 
            boolean is24HourFormat = use24HourFormat();
            new TimePickerDialog(DutyHoursForm.this, 
                    startTimeListener, 
                    startDateAndTime.get(Calendar.HOUR_OF_DAY), 
                    startDateAndTime.get(Calendar.MINUTE), 
                    is24HourFormat).show(); 
            } 
    }); 

TimePickerDialog.OnTimeSetListener startTimeListener = new TimePickerDialog.OnTimeSetListener() { 
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
        startDateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay); 
        startDateAndTime.set(Calendar.MINUTE, minute); 
        updateStartTime(); 
    } 
};

I'm looking for a way to dynamically change the AM/PM within a TimePickerDialog for my Android application while using 12 hour format. I'm hoping to toggle the AM to PM and visa versa when the hour changes from 11 to 12 within the dialog but before the user clicks th set button. Is this possible or will I need to look into creating a custom TimePickerDialog control? My current code is below. Thanks for the help!

    // Start Time
    starttime = (Button)findViewById(R.id.starttime); 

    starttime.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) { 
            boolean is24HourFormat = use24HourFormat();
            new TimePickerDialog(DutyHoursForm.this, 
                    startTimeListener, 
                    startDateAndTime.get(Calendar.HOUR_OF_DAY), 
                    startDateAndTime.get(Calendar.MINUTE), 
                    is24HourFormat).show(); 
            } 
    }); 

TimePickerDialog.OnTimeSetListener startTimeListener = new TimePickerDialog.OnTimeSetListener() { 
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
        startDateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay); 
        startDateAndTime.set(Calendar.MINUTE, minute); 
        updateStartTime(); 
    } 
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2024-10-27 14:29:29

初始化一个变量来跟踪更改之前的小时 - 例如lastCurrentHour
那么您可以在 onTimeChanged 中执行此操作:

public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
    if(DateFormat.is24HourFormat(this)) return;

    if(lastCurrentHour == 0 && view.getCurrentHour()==11 ){
        view.setCurrentHour(23);
    }else

    if(lastCurrentHour == 11 && view.getCurrentHour()==0 ){
        view.setCurrentHour(12);
    }else

    if(lastCurrentHour == 12 && view.getCurrentHour()==23 ){
        view.setCurrentHour(11);
    }else

    if(lastCurrentHour == 23 && view.getCurrentHour()==12 ){
        view.setCurrentHour(0);
    }
    lastCurrentHour = view.getCurrentHour();
}

此处找到一个工作示例。

Initialise a variable to keep track of the hour before the change - like lastCurrentHour
then you can do this in onTimeChanged:

public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
    if(DateFormat.is24HourFormat(this)) return;

    if(lastCurrentHour == 0 && view.getCurrentHour()==11 ){
        view.setCurrentHour(23);
    }else

    if(lastCurrentHour == 11 && view.getCurrentHour()==0 ){
        view.setCurrentHour(12);
    }else

    if(lastCurrentHour == 12 && view.getCurrentHour()==23 ){
        view.setCurrentHour(11);
    }else

    if(lastCurrentHour == 23 && view.getCurrentHour()==12 ){
        view.setCurrentHour(0);
    }
    lastCurrentHour = view.getCurrentHour();
}

Find a working example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文