Android 如何设置 TimePicker 显示格式为 24h
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12453075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set TimePicker show with format 24h
提问by mum
I've created a TimePicker in layout and I want it to show time with format 24h.
我在布局中创建了一个 TimePicker,我希望它以 24 小时格式显示时间。
Can you help me? Thanks.
你能帮助我吗?谢谢。
回答by VendettaDroid
TimePicker
is a view for selecting the time of day, in either 24 hour or AM/PM mode.
You can use setIs24HourView(true)
method with TimePicker.
TimePicker
是用于在 24 小时或 AM/PM 模式下选择一天中的时间的视图。您可以使用TimePicker 的setIs24HourView(true)
方法。
回答by BREMI
Setting TimePicker
by default to 24 hour mode is not always a good idea since many countrys have diffrent convention. For this case use DateFormat.is24HourFormat(getActivity())
.
设置TimePicker
在默认情况下24小时模式并不总是一个好主意,因为许多国家的具有不同势约定。对于这种情况,请使用DateFormat.is24HourFormat(getActivity())
.
Your final code migth look like picker.setIs24HourView(DateFormat.is24HourFormat(this));
.
您的最终代码可能看起来像picker.setIs24HourView(DateFormat.is24HourFormat(this));
.
Further details see:
更多详情请见:
回答by Jan Wrobel
If you want the TimePicker to be correctly initialized with the current time in 24h format use the following:
如果您希望使用 24 小时格式的当前时间正确初始化 TimePicker,请使用以下命令:
import java.util.Calendar;
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
Otherwise, due to Android bug, the picker will start with an incorrect hour (2 instead of 14 etc).
否则,由于 Android 错误,选择器将以错误的小时开始(2 而不是 14 等)。
回答by Daniel Susin
You just have to retrieve the TimePicker instance from the view after inflating it, then you can modify the widget.
您只需要在充气后从视图中检索 TimePicker 实例,然后您就可以修改小部件。
Ids are arbitrary:
Id 是任意的:
View v=getActivity().getLayoutInflater().inflate(R.layout.dialog_time, null);
TimePicker timePicker=(TimePicker)v.findViewById(R.id.dialog_time_timePicker);
timePicker.setIs24HourView(true);
回答by Daniel Susin
By default, it displays time in the AM/PM format. If you want to change time in the 24 hour format, then you can use the setIs24HourView() method. See this link: http://www.androidaspect.com/2012/06/timepicker-view-tutorial.html
默认情况下,它以 AM/PM 格式显示时间。如果要以 24 小时格式更改时间,则可以使用 setIs24HourView() 方法。请参阅此链接:http: //www.androidaspect.com/2012/06/timepicker-view-tutorial.html
回答by reza.cse08
xml
xml
<TimePicker
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/timePicker" />
java
爪哇
TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
回答by Hernan Ramovecchi
I need the same just for an application to set the duration spend for doing a task. I found that I just need to extend the TimePickerDialog class and pass True in the last constructor parameter boolean is24HourView.
我需要相同的应用程序来设置执行任务的持续时间。我发现我只需要扩展 TimePickerDialog 类并在最后一个构造函数参数 boolean is24HourView 中传递 True。
public class DurationPickerDialog extends TimePickerDialog {
public DurationPickerDialog(Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute){
super(context, callBack, hourOfDay, minute, true);
}
Then you only need to create this as
然后你只需要将它创建为
TimePickerDialog durationPicker = new DurationPickerDialog(getActivity(), this, hour, minute);
durationPicker.setTitle("Duration:");
回答by TheOmegaDog
Or you let the user decide for himselfwhich format he want to use.
或者您让用户自己决定他想使用哪种格式。
In your MainActivty:
在您的 MainActivity 中:
DialogFragment timePicker = new TimePickerFragment();
timePicker.show(getSupportFragmentManager(), "time picker");
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
TextView outputText= (TextView)findViewById(R.id.outputText);
outputText.setText(hourOfDay + " : "+ minute);
}
And in a new Java Class (TimePickerFragment):
在一个新的 Java 类 (TimePickerFragment) 中:
public class TimePickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(),(TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}}
回答by Ashav Kothari
For Time picker Dialog
对于时间选择器对话框
Timepicker dialog provides by android to select the time.
android 提供了 Timepicker 对话框来选择时间。
Java provides default functionality by passing is24HourView == true in TimePickerDialog constructor.
Java 通过在 TimePickerDialog 构造函数中传递 is24HourView == true 来提供默认功能。
public TimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute,boolean is24HourView) {
this(context, 0, listener, hourOfDay, minute, is24HourView);
}
Pass true for 24 hours or pass false to set time picker dialog in AM-PM format.
传递 true 24 小时或传递 false 以设置 AM-PM 格式的时间选择器对话框。
for ex: (Kotlin)
例如:(科特林)
private fun showTimePicker() {
val cal = Calendar.getInstance()
val dialog = TimePickerDialog(this, TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
Log.e("Time :","$hourOfDay:$minute")
}, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true) //here pass true for 24hrs view else false for AM-PM(12hrs view)
dialog.show()
}
回答by Hossein
You just need call and send trueto setIs24HourViewfunction.
您只需要调用并向setIs24HourView函数发送true即可。
TimePicker tpHourMin = (TimePicker) findViewById(R.id.timePicker);
tpHourMin.setIs24HourView(true);