如何在DialogFragment中设置分钟间隔

时间:2015-02-05 13:32:41

标签: java android android-fragments android-dialogfragment dialogfragment

我试图在Android DialogFragment中设置一个10分钟的间隔(谷歌建议),但我真的找不到办法。

这是片段类,它显示了对话框:

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等......

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

修改

我的意见如下:

enter image description here

1 个答案:

答案 0 :(得分:4)

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

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

enter image description here

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

这是一个完整的例子:

@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()));