我需要实现一个对话框,用户可以在其中输入以分钟和秒为单位的时间。例如:6分30秒。在那里我还需要一个分钟数的上限(即,分钟的范围是0-10,从0到60秒。)
但正如此处所讨论的,android TimePicker仅提供小时和分钟。我已经看过一些关于添加秒数的帖子,但没有关于删除小时和添加秒数的内容。可能也没办法对会议记录施加限制。
我看过一些帖子建议使用NumberPicker创建自定义时间选择器,但也注意到NumberPicker用于内部目的而不应使用。这似乎是一个Catch-22。
我可以尝试修改TimePicker或NumberPicker的源代码,但我并不了解理解它们如何工作以及维护自定义版本所需的时间。
我想这让我实现了一个自定义的Minutes-Seconds选择器。使用上下按钮移动数字看起来并不太难,但我不知道如何让用户滑动以滚动每个“滚轮”中的数字。
有什么建议吗?
答案 0 :(得分:5)
使用this SO answer为TimePickerDialog添加分钟间隔后,我创建了一个类似的子类,它将小时微调器的范围扩展到0-60,并将小时:分钟微调器视为分钟:秒:
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.NumberPicker;
import android.widget.TimePicker;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class DurationPicker extends TimePickerDialog {
private TimePicker timePicker;
private final OnTimeSetListener callback;
private int initialMinutes;
public DurationPicker(Context context, OnTimeSetListener callBack, int minutes, int seconds) {
super(context, callBack, minutes, seconds, true); // the last argument removes the am/pm spinner
this.callback = callBack;
this.initialMinutes = minutes; // we'll have to set this again after modifying the "hours" spinner
this.setTitle("Set duration");
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (callback != null && timePicker != null) {
timePicker.clearFocus();
callback.onTimeSet(timePicker, timePicker.getCurrentHour(), timePicker.getCurrentMinute());
}
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
try {
Class<?> classForid = Class.forName("com.android.internal.R$id");
Field timePickerField = classForid.getField("timePicker");
this.timePicker = (TimePicker) findViewById(timePickerField.getInt(null));
Field field = classForid.getField("hour");
// modify the hours spinner to cover the maximum number of minutes we want to support
int maxMinutes = 60;
NumberPicker mHourSpinner = (NumberPicker)timePicker.findViewById(field.getInt(null));
mHourSpinner.setMinValue(0);
mHourSpinner.setMaxValue(maxMinutes);
List<String> displayedValues = new ArrayList<String>();
for (int i = 0; i <= maxMinutes; i++) {
displayedValues.add(String.format("%d", i));
}
mHourSpinner.setDisplayedValues(displayedValues.toArray(new String[0]));
mHourSpinner.setValue(initialMinutes); // we can set this again now that we've modified the hours spinner
} catch (Exception e) {
e.printStackTrace();
}
}
}
在回调中,只需将第二个参数中的值视为分钟,将第三个参数中的值视为秒。
未来版本的Android可能会在&#34;分钟&#34;微调器设置为24以上的数字,但我读完了当前的TimePicker代码,并没有看到任何问题。此外,可以使用秒旋转器修改或替换am / pm微调器,然后拾取器可以支持小时:分钟:秒以及分钟:秒。
答案 1 :(得分:0)
不幸的是,@arlomedia 的代码对我不起作用,因为它没有找到 hour
字段。
我在 this 答案(Kotlin 版本)的帮助下修改了课程:
import android.app.TimePickerDialog
import android.content.Context
import android.content.DialogInterface
import android.content.res.Resources
import android.view.View
import android.widget.NumberPicker
import android.widget.TimePicker
import java.util.*
class DurationPickerDialog(context: Context?, private val callback: OnTimeSetListener?, private val initialMinutes: Int, seconds: Int) :
TimePickerDialog(context, callback, initialMinutes, seconds, true) {
init {
this.setTitle("Set duration")
}
private var timePicker: TimePicker? = null
override fun onClick(dialog: DialogInterface, which: Int) {
if (callback != null && timePicker != null) {
timePicker!!.clearFocus()
callback.onTimeSet(timePicker, timePicker!!.currentHour, timePicker!!.currentMinute)
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
try {
timePicker = findViewById<View>(
Resources.getSystem().getIdentifier("timePicker", "id", "android")
) as TimePicker
val hoursPicker = timePicker!!.findViewById(
Resources.getSystem().getIdentifier("hour", "id", "android")
) as NumberPicker
// Modify the hours spinner to cover the maximum number of minutes we want to support
val maxMinutes = 60
hoursPicker.minValue = 0
hoursPicker.maxValue = maxMinutes
val displayedValues: MutableList<String> = ArrayList()
for (i in 0..maxMinutes) displayedValues.add(String.format("%d", i))
hoursPicker.displayedValues = displayedValues.toTypedArray()
hoursPicker.value = initialMinutes
} catch (e: Exception) {
e.printStackTrace()
}
}
}