4

我需要实现一个对话框,用户可以在其中输入以分钟和秒为单位的时间。例如:6 分 30 秒。在那里,我还需要分钟数的上限(即,分钟的范围为 0-10,秒的范围为 0-60。)

但正如这里所讨论的,android TimePicker 只显示小时和分钟。我看过一些关于添加秒数的帖子,但没有关于删除小时数和添加秒数的帖子。可能也没有办法对分钟施加限制。

我看过一些建议使用 NumberPicker 创建自定义时间选择器的帖子,但也看到了 NumberPicker 仅供内部使用且不应使用的警告。这似乎是 Catch-22。

我可以尝试修改 TimePicker 或 NumberPicker 的源代码,但我并不热衷于了解它们的工作方式和维护自定义版本所需的时间。

我想这让我不得不实现一个自定义的 Minutes-Seconds 选择器。使用上下按钮移动数字看起来并不太难,但我不知道如何让用户滑动以滚动每个“轮”中的数字。

有什么建议么?

4

2 回答 2

6

在使用此 SO 答案向 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();
        }

    }

}

在回调中,只需将第二个参数的值视为分钟,将第三个参数的值视为秒。

当“分钟”微调器设置为大于 24 的数字时,未来版本的 Android 可能会返回不正确的结果,但我通读了当前的 TimePicker 代码并没有发现任何问题。此外,可以用秒微调器修改或替换上午/下午微调器,然后选择器可以支持小时:分钟:秒以及分钟:秒。

于 2014-07-27T02:18:36.033 回答
0

不幸的是,@arlomedia 的代码对我不起作用,因为它没有找到该hour字段。我在这个答案(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()
        }
    }
}
于 2021-02-10T12:09:38.003 回答