更改字段时,自定义TimePicker对话框标题会更改

时间:2015-11-05 09:15:07

标签: android field title timepicker

我已经创建了一个自定义TimePicker对话框,但是当我更改字段时,对话框的标题也会发生变化。这种情况不会发生在我尝试过的每部手机上。

运行 Android 5.1.1 OnePlus One 和运行 android 6.1 <的 LG Nexus 5 的标题保持不变/ strong>即可。当我在运行 android 5 三星Galaxy 5 上进行尝试时,它确实会发生变化。

知道是什么导致了这个吗?

我的自定义Timepicker类。

public class CustomTimePickerDialog extends TimePickerDialog {

    private  static int MINUTE_PICKER_INTERVAL = 60;
    private final static int HOUR_PICKER_INTERVAL = 1;
    private static int HOUR_PICKER_MIN;
    private static int HOUR_PICKER_MAX;
    private TimePicker timePicker;

    private final OnTimeSetListener callback;

    public CustomTimePickerDialog(Context context, OnTimeSetListener callBack,
                                  int hourOfDay, int minute, boolean is24HourView, int hourPickerMin, int hourPickerMax, int timedistribution) {
        super(context, TimePickerDialog.THEME_HOLO_LIGHT, callBack, hourOfDay, minute / MINUTE_PICKER_INTERVAL,
                is24HourView);
        this.callback = callBack;
        HOUR_PICKER_MIN = hourPickerMin;
        HOUR_PICKER_MAX = hourPickerMax;
        MINUTE_PICKER_INTERVAL = timedistribution;
        this.setTitle(context.getString(R.string.insert_time));
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (callback != null && timePicker != null) {

            timePicker.clearFocus();
            callback.onTimeSet(timePicker, timePicker.getCurrentHour(),
                    timePicker.getCurrentMinute() * MINUTE_PICKER_INTERVAL);
        }
    }


    @Override
    protected void onStop() {
        dismiss();
    }
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.getButton(CustomTimePickerDialog.BUTTON_POSITIVE).setText(getContext().getResources().getString(R.string.set));
        this.setButton(CustomTimePickerDialog.BUTTON_NEGATIVE, getContext().getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (which == DialogInterface.BUTTON_NEGATIVE) {
                }
            }
        });

        try {
            Class<?> classForid = Class.forName("com.android.internal.R$id");
            Field timePickerField = classForid.getField("timePicker");
            this.timePicker = (TimePicker) findViewById(timePickerField
                    .getInt(null));
            Field fieldHour = classForid.getField("hour");
            NumberPicker mHourSpinner = (NumberPicker) timePicker.findViewById(fieldHour.getInt(null));
            mHourSpinner.setMinValue(HOUR_PICKER_MIN);
            mHourSpinner.setMaxValue((HOUR_PICKER_MAX / HOUR_PICKER_INTERVAL) - 1);

            List<String> displayedValuesHour = new ArrayList<String>();
            for (int i = HOUR_PICKER_MIN; i < HOUR_PICKER_MAX; i += HOUR_PICKER_INTERVAL) {
                displayedValuesHour.add(String.format("%02d", i));
            }
            mHourSpinner.setDisplayedValues(displayedValuesHour
                    .toArray(new String[0]));



            Field fieldMinute = classForid.getField("minute");
            NumberPicker mMinuteSpinner = (NumberPicker) timePicker
                    .findViewById(fieldMinute.getInt(null));
            mMinuteSpinner.setMinValue(0);
            mMinuteSpinner.setMaxValue((60 / MINUTE_PICKER_INTERVAL) - 1);
            List<String> displayedValuesMinute = new ArrayList<String>();
            for (int i = 0; i < 60; i += MINUTE_PICKER_INTERVAL) {
                displayedValuesMinute.add(String.format("%02d", i));
            }
            mMinuteSpinner.setDisplayedValues(displayedValuesMinute
                    .toArray(new String[0]));


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

我的方法叫它。

private void showTimeDialog(final EditText toEdit, final int startOrEnd) {
        String currentTimeValue = toEdit.getText().toString();
        String currentHourValue = currentTimeValue.split(":", 2)[0];
        String currentMinValue = currentTimeValue.substring(currentTimeValue.indexOf(":") + 1);
        if (currentHourValue.equals("")) {
            currentHourValue = "00";
        }
        if (currentMinValue.equals("")) {
            currentMinValue = "00";
        }
        customTimePickerDialog = new CustomTimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hour, int min) {
                String oldStartTime = startsAtTime.getText().toString();
                String oldEndTime = endsAtTime.getText().toString();
                Calendar cal = Calendar.getInstance();
                try {
                    cal.setTime(DateFormatsConvert.stringToTimeFormat(BookingActivity.this, oldStartTime));
                    oldStartHour = cal.get(Calendar.HOUR_OF_DAY);
                    oldStartMin = cal.get(Calendar.MINUTE);
                    cal.setTime(DateFormatsConvert.stringToTimeFormat(BookingActivity.this, oldEndTime));
                    oldEndHour = cal.get(Calendar.HOUR_OF_DAY);
                    oldEndMin = cal.get(Calendar.MINUTE);
                    oldMinDifference = ((oldEndHour - oldStartHour) * 60) + (oldEndMin - oldStartMin);
                } catch (Exception e) {
                    oldMinDifference = 0;
                    e.printStackTrace();
                }
                if(min>endMinute && hour == endHour){
                        selectedTime = String.format("%02d:%02d", hour-1, min);
                        if(endMinute < 10){
                            CustomToast.makeText(BookingActivity.this, (getString(R.string.chosen_time_not_available).replace("[endHour]", Integer.toString(endHour))).replace("[endMinute]", "0" + Integer.toString(endMinute)), Toast.LENGTH_SHORT);

                        }
                        else {
                            CustomToast.makeText(BookingActivity.this, (getString(R.string.chosen_time_not_available).replace("[endHour]", Integer.toString(endHour))).replace("[endMinute]", Integer.toString(endMinute)), Toast.LENGTH_SHORT);
                        }
                }
                else {
                    selectedTime = String.format("%02d:%02d", hour, min);
                }
                toEdit.setText(selectedTime);
                try {
                    String newStartTime = startsAtTime.getText().toString();
                    String newEndTime = endsAtTime.getText().toString();
                    cal.setTime(DateFormatsConvert.stringToTimeFormat(BookingActivity.this, newStartTime));
                    newStartHour = cal.get(Calendar.HOUR_OF_DAY);
                    newStartMin = cal.get(Calendar.MINUTE);
                    cal.setTime(DateFormatsConvert.stringToTimeFormat(BookingActivity.this, newEndTime));
                    newEndHour = cal.get(Calendar.HOUR_OF_DAY);
                    newEndMin = cal.get(Calendar.MINUTE);
                    newMinDifference = ((newEndHour - newStartHour) * 60) + (newEndMin - newStartMin);
                    addDiffChange = newMinDifference - oldMinDifference;
                    chosenTimeBlocks = chosenTimeBlocks + addDiffChange / timeDistribution;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                switch (startOrEnd) {
                    case 1:
                        checkEndDate();
                        break;
                    case 2:
                        checkStartDate();
                        break;
                }
            }
        }, Integer.parseInt(currentHourValue), Integer.parseInt(currentMinValue), true, startHour, endHour + 1, timeDistribution);
        customTimePickerDialog.show();
    }

0 个答案:

没有答案