时间间隔为15分钟

时间:2015-09-04 12:18:00

标签: android layout custom-controls timepicker

我有一个带有timepicker和datepicker的自定义对话框。我想将timepicker的微小旋转器集合设置为[0,15,30,45],即间隔15分钟。

现在我看到了几个解决方案,但似乎没有一个解决这个事实,即用户界面将显示下一个和前一个分钟为-1和+1分钟的选定分钟,例如29,30,31向用户显示。

另外,我不清楚如何将自定义时间戳附加到活动 - 我不需要customerTimePicker xml组件来执行此操作吗?

所以简而言之 - 我是否需要调整本机时间戳或者需要构建自定义xml和类?

目前我有什么 布局:

<TimePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/timePicker_fixture"
    android:layout_weight="1" />

活动

TimePicker timePicker_fixture = (TimePicker) dialog.findViewById(R.id.timePicker_fixture);
timePicker_fixture.setIs24HourView(true);

建议的自定义类(我如何使用它 - 我的布局中是否需要自定义xml组件?)

public class CustomTimePickerDialog extends TimePickerDialog {

    private final static int TIME_PICKER_INTERVAL = 15;
    private TimePicker timePicker;
    private final OnTimeSetListener callback;

    public CustomTimePickerDialog(Context context, OnTimeSetListener callBack,
                                  int hourOfDay, int minute, boolean is24HourView) {
        super(context, TimePickerDialog.THEME_HOLO_LIGHT, callBack, hourOfDay, minute / TIME_PICKER_INTERVAL,
                is24HourView);
        this.callback = callBack;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (callback != null && timePicker != null) {
            timePicker.clearFocus();
            callback.onTimeSet(timePicker, timePicker.getCurrentHour(),
                    timePicker.getCurrentMinute() * TIME_PICKER_INTERVAL);
        }
    }

    @Override
    protected void onStop() {
    }

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

            NumberPicker mMinuteSpinner = (NumberPicker) timePicker
                    .findViewById(field.getInt(null));
            mMinuteSpinner.setMinValue(0);
            mMinuteSpinner.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));
            }
            mMinuteSpinner.setDisplayedValues(displayedValues
                    .toArray(new String[0]));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

4 个答案:

答案 0 :(得分:4)

添加了以下

   @SuppressLint("NewApi")
    private void setTimePickerInterval(TimePicker timePicker) {
        try {
            Class<?> classForid = Class.forName("com.android.internal.R$id");
            // Field timePickerField = classForid.getField("timePicker");

            Field field = classForid.getField("minute");
            minutePicker = (NumberPicker) timePicker
                    .findViewById(field.getInt(null));

            minutePicker.setMinValue(0);
            minutePicker.setMaxValue(7);
            displayedValues = new ArrayList<String>();
            for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
                displayedValues.add(String.format("%02d", i));
            }
            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) {
            e.printStackTrace();
        }
    }

在我的活动中,然后从oncreate()调用此方法。当然,您可以在必要时拨打电话。

答案 1 :(得分:0)

timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener()
{
    @Override
    public void onTimeChanged (TimePicker view, int hourOfDay, int minute)
    {
        view.setOnTimeChangedListener(null);
        int currentMinute = roundMinute(minute);
        view.setCurrentMinute(59);
        view.setCurrentMinute(currentMinute);
        view.setOnTimeChangedListener(this);
    }
});

private static int roundMinute (int minute)
{
    return minute == 0 ? 0 : minute/5*5;
}

答案 2 :(得分:0)

上面的答案在Android 7 Nougat中对我不起作用,基本上日期选择器完全被时钟模式改变了......我把这个代码与https://gist.github.com/jeffdgr8/6bc5f990bf0c13a7334ce385d482af9f合并,然后默认设置为TimePickerDialog的模式显示Spinner控件,然后它运行代码以仅显示15minuts间隔。

结果在这里:https://gist.github.com/smaugho/14dae83f3284fa05455ee0a9e4f13099

答案 3 :(得分:0)

/**
 * Set TimePicker interval by adding a custom minutes list
 *
 * @param timePicker
 */
private void setTimePickerInterval(TimePicker timePicker) {
    try {
        int TIME_PICKER_INTERVAL = 15;
        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);
    }
}