如何在android中设置自定义分钟间隔timepickerdialog

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在android中设置自定义分钟间隔timepickerdialog相关的知识,希望对你有一定的参考价值。

android如果要修改自定义分钟间隔,通常的做法是重写一个TimePickerDialog,继承它,不过也可以通过监听事件来监听数值的改变,来显示自定义的分钟间隔,代码如下:
private static final int TIME_PICKER_INTERVAL=15;
private boolean mIgnoreEvent=false;

private TimePicker.OnTimeChangedListener mTimePickerListener=new TimePicker.OnTimeChangedListener()
public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute)
if (mIgnoreEvent)
return;
if (minute%TIME_PICKER_INTERVAL!=0)
int minuteFloor=minute-(minute%TIME_PICKER_INTERVAL);
minute=minuteFloor + (minute==minuteFloor+1 ? TIME_PICKER_INTERVAL : 0);
if (minute==60)
minute=0;
mIgnoreEvent=true;
timePicker.setCurrentMinute(minute);
mIgnoreEvent=false;



;
参考技术A public class CustomTimePickerDialog extends TimePickerDialog
public static final int TIME_PICKER_INTERVAL=10;
private boolean mIgnoreEvent=false;
public CustomTimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute,
boolean is24HourView)
super(context, callBack, hourOfDay, minute, is24HourView);

/*
* (non-Javadoc)
* @see android.app.TimePickerDialog#onTimeChanged(android.widget.TimePicker, int, int)
* Implements Time Change Interval
*/
@Override
public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute)
super.onTimeChanged(timePicker, hourOfDay, minute);
this.setTitle("2. Select Time");
if (!mIgnoreEvent)
minute = getRoundedMinute(minute);
mIgnoreEvent=true;
timePicker.setCurrentMinute(minute);
mIgnoreEvent=false;


public static int getRoundedMinute(int minute)
if(minute % TIME_PICKER_INTERVAL != 0)
int minuteFloor = minute - (minute % TIME_PICKER_INTERVAL);
minute = minuteFloor + (minute == minuteFloor + 1 ? TIME_PICKER_INTERVAL : 0);
if (minute == 60) minute=0;

return minute;



这个类,所有你需要做的,而不是在你的代码来访问类/覆盖默认函数。在简单的方法,你会因为在此之后下... private CustomTimePickerDialog.OnTimeSetListener timeSetListener = new CustomTimePickerDialog.OnTimeSetListener()
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)

// using CustomTimePickerDialog本回答被提问者和网友采纳

以上是关于如何在android中设置自定义分钟间隔timepickerdialog的主要内容,如果未能解决你的问题,请参考以下文章

我如何在android中设置自定义日期

如何在 ActionBar 标题中设置自定义字体?

如何在 Oracle SQL Developer 中设置自定义日期时间格式?

如何在 iOS 的 WebView(react-native-webview) 中设置自定义字体?

怎么在excel中设置自定义日期格式

在 Argument 中设置自定义数据类型