我有一个TimePickerDialog工作来设置时间,设置为TextView以显示它。现在,我需要帮助将TimePicker(在TimePickerDialog内)的minutues间隔设置为15分钟。我已经看到有一个与TimePicker相关的15分钟间隔问题的帖子,但我不知道如何将它应用于TimePickerDialog,因为我不知道如何使用它在TimePickerDialog中创建的TimePicker。我是Android新手,完全迷失在这件事上。提前谢谢。
答案 0 :(得分:14)
使用来自@Rizwan和this其他线程的this的组合,我提出了一个组合解决方案,允许TimePickerDialog
中的任意分钟增量。主要问题是大多数功能都被android TimePickerDialog
和TimePicker
类隐藏,并且它似乎不是
TimePickerDialog
以便我们更轻松地访问TimePicker
以接收和返回NumberPicker
尊重我们自定义增量的值。到达UI内部的主要问题是元素由可能更改的ID引用,甚至id的名称也不能保证永远相同。话虽如此,这是一个有效,稳定的解决方案,并且可能在可预见的未来发挥作用。在我看来,空的catch块应该警告UI已经改变并且应该回到默认(增量1分钟)行为。
private class DurationTimePickDialog extends TimePickerDialog
{
final OnTimeSetListener mCallback;
TimePicker mTimePicker;
final int increment;
public DurationTimePickDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, int increment)
{
super(context, callBack, hourOfDay, minute/increment, is24HourView);
this.mCallback = callBack;
this.increment = increment;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (mCallback != null && mTimePicker!=null) {
mTimePicker.clearFocus();
mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
mTimePicker.getCurrentMinute()*increment);
}
}
@Override
protected void onStop()
{
// override and do nothing
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try
{
Class<?> rClass = Class.forName("com.android.internal.R$id");
Field timePicker = rClass.getField("timePicker");
this.mTimePicker = (TimePicker)findViewById(timePicker.getInt(null));
Field m = rClass.getField("minute");
NumberPicker mMinuteSpinner = (NumberPicker)mTimePicker.findViewById(m.getInt(null));
mMinuteSpinner.setMinValue(0);
mMinuteSpinner.setMaxValue((60/increment)-1);
List<String> displayedValues = new ArrayList<String>();
for(int i=0;i<60;i+=increment)
{
displayedValues.add(String.format("%02d", i));
}
mMinuteSpinner.setDisplayedValues(displayedValues.toArray(new String[0]));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
构造函数接受增量值并保留其他一些引用。请注意,这省略了错误检查,我们更喜欢60%increment==0
onCreate 使用UI字段和反射的名称来发现当前位置。这再次省略了错误检查,应该是“故障安全”,即如果出现问题则恢复默认行为。
onClick 重写以将正确的分钟值返回给回调侦听器
重写onStop 以防止在对话框关闭时第二次返回(不正确的)索引值。继续,亲自尝试。
其中大部分来自于挖掘TimePickerDialog源代码。
答案 1 :(得分:11)
如果您使用时间选择器而不是时间选择器对话框那么这很好。但实际上有一个解决方案..这是我用来满足相同要求的..我使用CustomTimePickerDialog。每件事都是一样的,只有TimePickerDialog会在代码中更改为CustomTimePickerDialog。
CustomTimePickerDialog timePickerDialog = new CustomTimePickerDialog(myActivity.this, timeSetListener,
Calendar.getInstance().get(Calendar.HOUR),
CustomTimePickerDialog.getRoundedMinute(Calendar.getInstance().get(Calendar.MINUTE) + CustomTimePickerDialog.TIME_PICKER_INTERVAL),
false
);
timePickerDialog.setTitle("2. Select Time");
timePickerDialog.show();
这是我的CustomTimePickerDialog类...只需在项目中使用此类并将TimePickerDialog更改为CustomTimePickerDialog ..
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;
}
}
使用此CustomTimePickerDialog类之后,您需要做的只是在代码中使用CustomTimePickerDialog而不是TimePickerDialog来访问/覆盖TimePickerDialog类的默认函数。简单来说,我的意思是你的timeSetListener将在此之后如下......
private CustomTimePickerDialog.OnTimeSetListener timeSetListener = new CustomTimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}// using CustomTimePickerDialog
答案 2 :(得分:1)
您可以使用常规AlertDialog并使用setView包含自定义TimePicker视图。
答案 3 :(得分:0)
/**
* Set TimePicker interval by adding a custom minutes list
* TIME_PICKER_INTERVAL = Enter your Minutes;
* @param timePicker
*/
private void setTimePickerInterval(TimePicker timePicker) {
try {
int TIME_PICKER_INTERVAL = 10;
NumberPicker minutePicker = (NumberPicker) timePicker.findViewById(Resources.getSystem().getIdentifier(
"minute", "id", "android"));
minutePicker.setMinValue(0);
minutePicker.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
List<String> displayedValues = new ArrayList<String>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
displayedValues.add(String.format("%02d", i));
}
minutePicker.setDisplayedValues(displayedValues.toArray(new String[0]));
} catch (Exception e) {
Log.e(TAG, "Exception: " + e);
}
}