如何在TimePickerDialog android中禁用键盘?

时间:2017-12-29 10:54:41

标签: android android-datepicker android-timepicker

我在我的应用中使用timepicker和datepicker。我想在这两个中禁用键盘。我可以使用以下代码禁用日期选择器中的键盘。

datePickerDialog.getDatePicker().setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

但没有得到如何在timepickerdialog中禁用键盘。

我尝试过以下流行的例子,但仍然没有用

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener
{
    private TimePickerDialog timePickerDialog;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        //Default 6:00
        timePickerDialog = new TimePickerDialog(getActivity(), this, 6, 0, android.text.format.DateFormat.is24HourFormat(getActivity()));

        return timePickerDialog;
    }

    @Override
    public void onStart()
    {
        super.onStart();

        final View hourView = timePickerDialog.findViewById(Resources.getSystem().getIdentifier("hour", "id", "android"));
        final View minuteView = timePickerDialog.findViewById(Resources.getSystem().getIdentifier("minute", "id", "android"));

        if (hourView != null && hourView instanceof NumberPicker)
        {
            ((NumberPicker) hourView).setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
        }

        if (minuteView != null && minuteView instanceof NumberPicker)
        {
            ((NumberPicker) minuteView).setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
        }
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute)
    {
        ((MenuEditActivity) getActivity()).setRecipeTime(hourOfDay, minute);
    }

    @Override
    public void onCancel(DialogInterface dialog)
    {
        super.onCancel(dialog);
    }
}

任何帮助?

3 个答案:

答案 0 :(得分:1)

通过创建自定义Dialog并扩展TimePickerDialogClass

解决了这个问题
public class CustomTimePickerDialog extends TimePickerDialog {



private int currentHour = 0;
private int currentMinute = 0;

private Calendar calendar = Calendar.getInstance();
private DateFormat dateFormat;


public RangeTimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) {
    super(context, callBack, hourOfDay, minute, is24HourView);
    currentHour = hourOfDay;
    currentMinute = minute;
    dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);

    try {
        Class<?> superclass = getClass().getSuperclass();
        Field mTimePickerField = superclass.getDeclaredField("mTimePicker");
        mTimePickerField.setAccessible(true);
        TimePicker mTimePicker = (TimePicker) mTimePickerField.get(this);
        mTimePicker.setOnTimeChangedListener(this);
    } catch (NoSuchFieldException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    }

    fixSpinner(context, hourOfDay, minute, is24HourView);
}



@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {



    updateTime(currentHour, currentMinute);
    updateDialogTitle(view, currentHour, currentMinute);
}

private void updateDialogTitle(TimePicker timePicker, int hourOfDay, int minute) {
    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calendar.set(Calendar.MINUTE, minute);
    String title = dateFormat.format(calendar.getTime());
    setTitle(title);
}


private void fixSpinner(Context context, int hourOfDay, int minute, boolean is24HourView) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // android:timePickerMode spinner and clock began in Lollipop
        try {
            // Get the theme's android:timePickerMode
            final int MODE_SPINNER = 2;
            Class<?> styleableClass = Class.forName("com.android.internal.R$styleable");
            Field timePickerStyleableField = styleableClass.getField("TimePicker");
            int[] timePickerStyleable = (int[]) timePickerStyleableField.get(null);
            final TypedArray a = context.obtainStyledAttributes(null, timePickerStyleable, android.R.attr.timePickerStyle, 0);
            Field timePickerModeStyleableField = styleableClass.getField("TimePicker_timePickerMode");
            int timePickerModeStyleable = timePickerModeStyleableField.getInt(null);
            final int mode = a.getInt(timePickerModeStyleable, MODE_SPINNER);
            a.recycle();
            if (mode == MODE_SPINNER) {
                TimePicker timePicker = (TimePicker) findField(TimePickerDialog.class, TimePicker.class, "mTimePicker").get(this);
                Class<?> delegateClass = Class.forName("android.widget.TimePicker$TimePickerDelegate");
                Field delegateField = findField(TimePicker.class, delegateClass, "mDelegate");
                Object delegate = delegateField.get(timePicker);
                Class<?> spinnerDelegateClass;
                if (Build.VERSION.SDK_INT != Build.VERSION_CODES.LOLLIPOP) {
                    spinnerDelegateClass = Class.forName("android.widget.TimePickerSpinnerDelegate");
                } else {
                    // TimePickerSpinnerDelegate was initially misnamed TimePickerClockDelegate in API 21!
                    spinnerDelegateClass = Class.forName("android.widget.TimePickerClockDelegate");
                }
                // In 7.0 Nougat for some reason the timePickerMode is ignored and the delegate is TimePickerClockDelegate
                if (delegate.getClass() != spinnerDelegateClass) {
                    delegateField.set(timePicker, null); // throw out the TimePickerClockDelegate!
                    timePicker.removeAllViews(); // remove the TimePickerClockDelegate views
                    Constructor spinnerDelegateConstructor = spinnerDelegateClass.getConstructor(TimePicker.class, Context.class, AttributeSet.class, int.class, int.class);
                    spinnerDelegateConstructor.setAccessible(true);
                    // Instantiate a TimePickerSpinnerDelegate
                    delegate = spinnerDelegateConstructor.newInstance(timePicker, context, null, android.R.attr.timePickerStyle, 0);
                    delegateField.set(timePicker, delegate); // set the TimePicker.mDelegate to the spinner delegate
                    // Set up the TimePicker again, with the TimePickerSpinnerDelegate
                    timePicker.setIs24HourView(is24HourView);
                    timePicker.setCurrentHour(hourOfDay);
                    timePicker.setCurrentMinute(minute);
                    timePicker.setOnTimeChangedListener(this);

timePicker.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

private static Field findField(Class objectClass, Class fieldClass, String expectedName) {
    try {
        Field field = objectClass.getDeclaredField(expectedName);
        field.setAccessible(true);
        return field;
    } catch (NoSuchFieldException e) {} // ignore
    // search for it if it wasn't found under the expected ivar name
    for (Field searchField : objectClass.getDeclaredFields()) {
        if (searchField.getType() == fieldClass) {
            searchField.setAccessible(true);

            return searchField;
        }
    }
    return null;
}

}

fixSpinner()功能

中添加了以下代码

<强> timePicker.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS)

并将其用作

 CustomTimePickerDialog timePickerDialog;
 timePickerDialog=new CustomTimePickerDialog (context,
                            timePickerListener, hour, minute,false);

 timePickerDialog.show();
 private TimePickerDialog.OnTimeSetListener timePickerListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int selectedHour,
                                  int selectedMinute) {
                hour = selectedHour;
                minute = selectedMinute;

            }
        };

答案 1 :(得分:0)

试试这个

public void ADDHideSoftKeyboard() {
        InputMethodManager lIMM;
        try {
            lIMM = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            lIMM.hideSoftInputFromWindow(pView.getWindowToken(), 0);
        }
    }

答案 2 :(得分:0)

将其用于时间选择器

public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
            TextView lTextViewTime;

            public TimePickerFragment(TextView pTextViewTime) {
                lTextViewTime = pTextViewTime;
            }

            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                final Calendar lCal = Calendar.getInstance();
                int lHour, lMinute;
                lHour = lCal.get(Calendar.HOUR_OF_DAY);
                lMinute = lCal.get(Calendar.MINUTE);
                return new TimePickerDialog(getActivity(), this, lHour, lMinute, DateFormat.is24HourFormat(getActivity()));
            }

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                lTextViewTime.setText(((hourOfDay < 10) ? ("0" + hourOfDay) : (hourOfDay)) + ":" + ((minute < 10) ? ("0" + minute) : (minute)));
            }
        }