如何在 DialogFragment 中设置一分钟间隔

How can I set a minute interval in DialogFragment

我正在尝试在 android DialogFragment 中设置 10 分钟的间隔(由 google 建议),但我真的找不到办法做到这一点。

这是显示对话框的片段 class:

public class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        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(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        String realMinute= "";

        switch (minute) {
            case 1:
                realMinute = "01";
                break;
            case 2:
                realMinute = "02";
                break;
            case 3:
                realMinute = "03";
                break;
            case 4:
                realMinute = "04";
                break;
            case 5:
                realMinute = "05";
                break;
            case 6:
                realMinute = "06";
                break;
            case 7:
                realMinute = "07";
                break;
            case 8:
                realMinute = "08";
                break;
            case 9:
                realMinute = "09";
                break;
            default:
                realMinute = String.valueOf(minute);
                break;
        }

        final String selectedDate = String.valueOf(hourOfDay) + ":" + realMinute;
        EditText selectedTimeTxt = (EditText) getActivity().findViewById(R.id.selectedTime);
        selectedTimeTxt.setText(selectedDate);
    }

一切都按预期工作,但我需要的是允许用户仅选择 10 分钟的间隔 - 例如 1:10、1:20、1:30、1:40等...

是否可以修改它,或者以某种方式扩展它以满足我的需要?我知道我在这里遗漏了一个基本点,但作为新手我无法发现它。

编辑

我的观点是:

您可以通过调用setDisplayedValues将您自己的值设置到第二个数字选择器(分钟数字选择器)。但是,您首先需要找到 NumberPicker 视图。

Android Device Monitor,有一个很好的功能,可以捕获设备的屏幕和单独的视图,因此您可以找到所需视图的 ID(或路径):

由于您的新值数组包含 6 个值 (0...5),因此您需要将当前分钟数转换为 tens,这样您就不会超出范围。

这是一个完整的例子:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    // Convert current minutes to tens
    // 55 = 50, 56 = 00
    int minute = c.get(Calendar.MINUTE) / 10;
    minute = (minute > 5) ? 0 : minute;

    // Create a new instance of TimePickerDialog and return it
    final TimePickerDialog tpd = new TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity()));

    tpd.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            int tpLayoutId = getResources().getIdentifier("timePickerLayout", "id", "android");

            ViewGroup tpLayout = (ViewGroup) tpd.findViewById(tpLayoutId);
            ViewGroup layout = (ViewGroup) tpLayout.getChildAt(0);

            // Customize minute NumberPicker
            NumberPicker minutePicker = (NumberPicker) layout.getChildAt(2);
            minutePicker.setDisplayedValues(new String[]{"00", "10", "20", "30", "40", "50"});
            minutePicker.setMinValue(0);
            minutePicker.setMaxValue(5);
        }
    });

    return tpd;
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    minute = minute * 10;
    Toast.makeText(getActivity(), "Selected minute: " + minute, Toast.LENGTH_LONG).show();
}

编辑:

使用AppCompat主题时,对话框会显示圆形滑块,不可自定义。但是,您可以告诉对话框使用更新的主题(这不会有问题,因为您的 minSdk 是 15):

final TimePickerDialog tpd = new TimePickerDialog(getActivity(),
        android.R.style.Theme_Holo_Light_Dialog, this, hour, minute,
        DateFormat.is24HourFormat(getActivity()));