Android N,L及以下时间戳,分钟间隔

时间:2016-07-22 15:37:46

标签: android android-timepicker android-7.0-nougat

我使用的Timepickerthis回答的间隔为一分钟。我在Andorid N预览5模拟器上运行此操作,显然THEME_HOLO_LIGHT Timepicker已被删除。

如何以Timepicker为间隔?我似乎不太可能会回到Android N的最终版本中。

1 个答案:

答案 0 :(得分:1)

确实创建我自己的时间戳是解决方案。我正在为其他人发布我的解决方案。这将创建一个时间戳对话框,其中包含api 14及更高版本的分钟间隔。它的工作间隔为1,5和15.其他值也可能有效。

用法:

Dialog intervalTimePickerDialog =  IntervalTimePickerDialogFactory.getIntervalTimePickerDialog(Context context, int positiveTextId, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, final int interval);
intervalTimePickerDialog .show();

我认为不需要太多解释。 int positiveTextId可以删除,这只是我需要的东西。

timepickerdialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TimePicker
        android:timePickerMode="spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/timePicker"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timePicker"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel"
            style="?android:attr/borderlessButtonStyle"
            android:id="@+id/buttonNegative" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Inklokken"
            style="?android:attr/borderlessButtonStyle"
            android:id="@+id/buttonPositive" />
    </LinearLayout>
</RelativeLayout>

代码:

public class IntervalTimePickerDialogFactory {
    public static Dialog getIntervalTimePickerDialog(Context context, int positiveTextId, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, final int interval){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            return new LIntervalTimePickerDialog(context, positiveTextId, callBack, hourOfDay, minute, is24HourView, interval);
        }else{
            return new IntervalTimePickerDialog(context, positiveTextId, callBack, hourOfDay, minute, is24HourView, interval);
        }
    }

    private static class IntervalTimePickerDialog extends TimePickerDialog {

        private TimePicker timePicker;
        private final OnTimeSetListener callback;
        private int interval;

        public IntervalTimePickerDialog(Context context, int positiveTextId, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, final int interval) {

            super(context, TimePickerDialog.THEME_HOLO_LIGHT, callBack, hourOfDay, ((int)Math.ceil((double)minute/(double)interval)), is24HourView);
            this.interval = interval;

            this.callback = callBack;

            this.setButton(BUTTON_POSITIVE, context.getResources().getString(positiveTextId), new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (callback != null && timePicker != null) {
                        timePicker.clearFocus();
                        callback.onTimeSet(timePicker, timePicker.getCurrentHour(),
                                timePicker.getCurrentMinute() * interval);
                    }
                }
            });
            this.setButton(BUTTON_NEGATIVE, context.getResources().getString(R.string.annuleren), new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dismiss();
                }
            });
        }


        @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 / interval) - 1);
                List<String> displayedValues = new ArrayList<>();
                for (int i = 0; i < 60; i += interval) {
                    displayedValues.add(String.format("%02d", i));
                }
                mMinuteSpinner.setDisplayedValues(displayedValues
                        .toArray(new String[0]));
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    private static class LIntervalTimePickerDialog extends Dialog{
        private static final DecimalFormat FORMATTER = new DecimalFormat("00");
        private final TimePickerDialog.OnTimeSetListener callback;
        private int interval;
        private TimePicker timePicker;
        private NumberPicker minutePicker;

        public LIntervalTimePickerDialog(Context context, int positiveTextId, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, final int interval) {
            super(context);
            setContentView(R.layout.timepickerdialog);
            timePicker = (TimePicker) findViewById(R.id.timePicker);
            timePicker.setIs24HourView(is24HourView);
            timePicker.setCurrentHour(hourOfDay);

            this.callback = callBack;
            this.interval = interval;

            Button buttonPositive = (Button) findViewById(R.id.buttonPositive);
            buttonPositive.setText(positiveTextId);
            buttonPositive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (callback != null && timePicker != null) {
                        timePicker.clearFocus();
                        callback.onTimeSet(timePicker, timePicker.getCurrentHour(),
                                timePicker.getCurrentMinute() * interval);
                    }
                    dismiss();
                }
            });

            Button buttonNegative = (Button) findViewById(R.id.buttonNegative);
            buttonNegative.setText(context.getResources().getString(R.string.annuleren));
            buttonNegative.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });

            setMinutePicker();
            timePicker.setCurrentMinute((int) Math.ceil((double) minute / (double) interval));
        }

        public void setMinutePicker() {
            int numValues = 60 / interval;
            String[] displayedValues = new String[numValues];
            for (int i = 0; i < numValues; i++) {
                displayedValues[i] = FORMATTER.format(i * interval);
            }

            View minute = timePicker.findViewById(Resources.getSystem().getIdentifier("minute", "id", "android"));
            if ((minute != null) && (minute instanceof NumberPicker)) {
                minutePicker = (NumberPicker) minute;
                minutePicker.setMinValue(0);
                minutePicker.setMaxValue(numValues - 1);
                minutePicker.setDisplayedValues(displayedValues);
            }
        }
    }
}