继续以下链接中提到的问题:
我可以解决这个问题但它只适用于你使用实际的键盘.如果您使用软键盘,我无法使用它.
我正在编写1.6版本的框架.
任何帮助都感激不尽.
PS:事实上,我到处寻找答案而无法找到它,这告诉我这一定非常简单或非常复杂.救命?!
我是一名法国 Android 开发人员,因此使用Locale.getDefault()会导致我DateFormat使用 24 小时模式。但是,当我通过设置菜单DateFormat将设备手动设置为 12 小时模式时,会继续以 24 小时格式运行。
相反,TimePickers 是根据我自己的 12/24 小时设置来设置的。
有没有办法让DateFormats 的行为与 s 相同TimePicker?
编辑:
这是我的DateFormat声明:
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
Run Code Online (Sandbox Code Playgroud)
这是我将我设置TimePicker为 12 或 24 小时模式的地方。
tp.setIs24HourView(android.text.format.DateFormat.is24HourFormat((Context) this));
Run Code Online (Sandbox Code Playgroud)
我的解决方案:
根据下面@Meno Hochschild 的回答,这是我解决这个棘手问题的方法:
boolean is24hour = android.text.format.DateFormat.is24HourFormat((Context) this);
tp.setIs24HourView(is24hour); // tp is the TimePicker
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
if (timeFormat instanceof SimpleDateFormat) {
String pattern = ((SimpleDateFormat) timeFormat).toPattern();
if (is24hour) {
timeFormat = …Run Code Online (Sandbox Code Playgroud) 我想创建一个像这样的DatePicker:
可能没有使用外部库.首先,有可能吗?现在我可以用这种方式创建一个简单的DatePickerDialog:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public EditText activity_edittext;
public DatePickerFragment(EditText edit_text) {
activity_edittext = edit_text;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int …Run Code Online (Sandbox Code Playgroud) java android datepicker android-dialogfragment android-timepicker
我有一个带有“ TimePreference”类的TimePickerDialog首选项,该类扩展了DialogPreference:
public class TimePreference extends DialogPreference{
...
private TimePicker picker=null;
...
public TimePreference(Context ctxt, AttributeSet attrs){
super(ctxt, attrs);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}
@Override
protected View onCreateDialogView(){
picker=new TimePicker(getContext());
return(picker);
}
...
}
Run Code Online (Sandbox Code Playgroud)
我想添加第三个按钮,它将把首选项设置为当前时间并关闭对话框,如下所示:

问题是,是否可以使用TimePickers进行此操作,还是应该自定义AlertDialog?
我有 12:00 PM 到 12:00 AM 的时间间隔。我想在时间选择器对话框中设置这个时间间隔。在时间选择器中禁用 12:00 PM 之前和 12:00 AM 之后的时间。我不知道如何在时间选择器对话框中设置时间。有人知道可以帮我解决这个问题吗?
时间选择器
private void showTimePicker(){
Time_picker time = new Time_picker();
Bundle args = new Bundle();
args.putInt("hours", c.get(Calendar.HOUR_OF_DAY)+2);
args.putInt("minute", c.get(Calendar.MINUTE)+30);
time.setArguments(args);
time.setCallBack(ontime);
time.show(((FragmentActivity) getActivity()).getSupportFragmentManager(), "Time Picker");
}
OnTimeSetListener ontime = new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
try {
String _24HourTime = String.valueOf(hourOfDay)+":"+String.valueOf(minute);
SimpleDateFormat _24HourSDF = new SimpleDateFormat("HH:mm");
SimpleDateFormat _12HourSDF = new SimpleDateFormat("hh:mm a");
Date _24HourDt = _24HourSDF.parse(_24HourTime); …Run Code Online (Sandbox Code Playgroud) 我最近在 EPG 应用程序中发现了一个(自定义)UI 元素,我想知道如何实现类似的功能。

基本上:使用这个 ui 元素,您可以选择一个时间。它只显示整小时和半小时,但您可以在(整个)屏幕上垂直滑动时选择 5 分钟间隔。如果您选择时间,应用程序会根据所选时间更新 UI。
该 UI 元素位于ListView. 垂直滑动可让您在列表中滚动,水平滑动(在整个屏幕上)可更改时间值。
我想实现类似的东西,但我完全不知道从哪里开始。我的主要问题是,如何实现时间列表并为其设置动画?(假设从今天开始到两天后)。我应该尝试延长ListView这一时间吗?或者TimePicker?Scrubber?
编辑:(想法1)
android android-custom-view android-listview android-timepicker
如何控制TimePicker,我只想显示从上午08:00到下午02:00的时间范围(从上午08:00到下午02:00隐藏所有)
我已经编写了完整的代码来显示TimePicker和自定义时间.
这是我的代码:
case DIALOG_TIME:
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(this, lisTime, hour, minute,
DateFormat.is24HourFormat(MainActivity.this));
TimePickerDialog.OnTimeSetListener lisTime = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
String meridiem = "";
nim = minute;
Calendar datetime = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
datetime.set(Calendar.MINUTE, minute);
if (datetime.get(Calendar.AM_PM) == Calendar.AM)
meridiem = "AM";
else if …Run Code Online (Sandbox Code Playgroud) 在我的项目中,我使用TimepickerDialog在TextView. 我Timepickerdialog的显示为时钟,但我希望它显示为微调器。我怎样才能做到这一点?
到目前为止,我设法像这样更改背景颜色:
<style name="DialogTheme" parent="Theme.MaterialComponents.DayNight.Dialog" >
<item name="android:backgroundTint">#141414</item>
</style>
Run Code Online (Sandbox Code Playgroud)
并像这样应用它:
TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), R.style.DialogTheme, listener, hour, minute, false);
Run Code Online (Sandbox Code Playgroud)
我正在运行 Android Pie。