android-如何设置格式为24h的TimePicker显示
我已经在布局中创建了一个TimePicker,我希望它以24h格式显示时间。
你能帮助我吗? 谢谢。
mum asked 2020-07-06T14:08:42Z
10个解决方案
74 votes
setIs24HourView(true)是用于选择24小时或AM / PM模式下的一天中的时间的视图。您可以将setIs24HourView(true)方法与TimePicker结合使用。
VendettaDroid answered 2020-07-06T14:09:01Z
38 votes
默认将picker.setIs24HourView(DateFormat.is24HourFormat(this));设置为24小时模式并不总是一个好主意,因为许多国家/地区使用不同的约定。 对于这种情况,请使用DateFormat.is24HourFormat(getActivity())。
您的最终代码可能看起来像picker.setIs24HourView(DateFormat.is24HourFormat(this));。
更多详细信息,请参见:
[HTTP://developer.Android.com/贵的/topics/UI/controls/pickers.HTML]
[HTTP://developer.Android.com/reference/Android/widget/time picker.HTML]
[HTTP://developer.Android.com/reference/Android/text/format/date format.HTML]
BREMI answered 2020-07-06T14:09:43Z
20 votes
如果您要使用当前时间正确地以24h格式正确初始化TimePicker,请使用以下命令:
import java.util.Calendar;
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
否则,由于Android错误,选择器将以错误的小时数开始(2而不是14等)。
Jan Wrobel answered 2020-07-06T14:10:07Z
4 votes
膨胀后,您只需要从视图中检索TimePicker实例,然后即可修改小部件。
ID是任意的:
View v=getActivity().getLayoutInflater().inflate(R.layout.dialog_time, null);
TimePicker timePicker=(TimePicker)v.findViewById(R.id.dialog_time_timePicker);
timePicker.setIs24HourView(true);
Daniel Susin answered 2020-07-06T14:10:36Z
1 votes
默认情况下,它以AM / PM格式显示时间。 如果要以24小时格式更改时间,则可以使用setIs24HourView()方法。看到此链接:[http://www.androidaspect.com/2012/06/timepicker-view-tutorial.html]
answered 2020-07-06T14:10:57Z
1 votes
XML文件
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/timePicker" />
爪哇
TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
reza.cse08 answered 2020-07-06T14:11:21Z
1 votes
我只需要为应用程序设置执行任务所需的时间。 我发现我只需要扩展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);
}
然后,您只需要创建为
TimePickerDialog durationPicker = new DurationPickerDialog(getActivity(), this, hour, minute);
durationPicker.setTitle("Duration:");
Hernan Ramovecchi answered 2020-07-06T14:11:48Z
0 votes
您只需要调用并将true发送给setIs24HourView函数即可。
TimePicker tpHourMin = (TimePicker) findViewById(R.id.timePicker);
tpHourMin.setIs24HourView(true);
Hossein answered 2020-07-06T14:12:08Z
0 votes
正如某些人已经提到的,只需将参数设置为true或false。
timePicker.setIs24HourView(true);
您可以找到带有源代码的完整示例,可从此处下载[http://www.ahotbrew.com/android-timepicker-example/]
Gurinder Singh answered 2020-07-06T14:12:38Z
0 votes
或者让用户自己决定要使用哪种格式。
在您的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);
}
并在新的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()));
}}
TheOmegaDog answered 2020-07-06T14:13:09Z