在 DatePickerDialog 的 onDateChanged 上隐藏确定按钮

Hide Ok button on onDateChanged in DatePickerDialog

我想在更改日期时隐藏确定按钮。下面是我写的代码,但按钮没有改变:

datePickerDialog.getDatePicker().init(year, month, day, new DatePicker.OnDateChangedListener() {

                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        Toast.makeText(getActivity(),
                                "onDateChanged " + "Year: " + year + "\n" +
                                        "Month of Year: " + monthOfYear + "\n" +
                                        "Day of Month: " + dayOfMonth, Toast.LENGTH_SHORT).show();


                        datePickerDialog.setButton(DatePickerDialog.BUTTON_POSITIVE, null, datePickerDialog);
                        datePickerDialog.setButton(DatePickerDialog.BUTTON_NEGATIVE, "CANCEL", datePickerDialog);


                    }});

非常感谢任何帮助。

通过隐藏确定按钮,您不希望用户 select 日期。 DatePicker 不是您的自定义对话框,这就是为什么很难隐藏按钮的原因。您可以限制用户点击确定按钮

    datePickerDialog.getDatePicker().init(year, month, day, new DatePicker.OnDateChangedListener() {

                        @Override
                        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            Toast.makeText(getActivity(),
                                    "onDateChanged " + "Year: " + year + "\n" +
                                            "Month of Year: " + monthOfYear + "\n" +
                                            "Day of Month: " + dayOfMonth, Toast.LENGTH_SHORT).show();


  datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,
                        "OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                if ("Your date condition") {
                                 // perform your action
                                }
                            }
                        });
                            datePickerDialog.setButton(DatePickerDialog.BUTTON_NEGATIVE, "CANCEL", datePickerDialog);


                        }});

这对我有用!

datePickerDialog.getButton(DatePickerDialog.BUTTON_POSITIVE).setVisibility(View.GONE);