如何在Datepicker对话框中禁用日期选择

时间:2018-03-25 14:51:33

标签: android datepicker android-datepicker

我想在日期选择器对话框中禁用日期选择。我使用下面的行禁用但我得到崩溃(空指针异常)。请帮我解决这个问题

((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);

完整代码:

Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {

                        tvDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

                    }
                }, mYear, mMonth, mDay);
        datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
        ((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
        datePickerDialog.show();

1 个答案:

答案 0 :(得分:0)

如果您使用的是DD / MM / YYYY

 LinearLayout pickerParentLayout = (LinearLayout) datePicker.getChildAt(0);
 LinearLayout pickerSpinnersHolder = (LinearLayout) pickerParentLayout.getChildAt(0);
 NumberPicker picker = (NumberPicker) pickerSpinnersHolder.getChildAt(0);
 picker.setVisibility(View.GONE);

NumberPicker选择器=(NumberPicker)pickerSpinnersHolder.getChildAt(0);  更改此行要隐藏的内容。