Android DatePickerDialog取消按钮不起作用

时间:2014-02-03 12:11:50

标签: android

我正在使用DatePicker Dialog。 当按钮单击datepicker显示时 一切都好。

但我的BUTTON_NEGATIVE按钮无效。我点击NEGATIVE_BUTTON,diloagpicker关闭但日期设置。

这是我的CalendarItem.java文件:

    public class CalendarItem extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        private Button pregnant_button;
        private Button numbered_button;

        public CalendarItem(Button pregnant_button) {
            this.pregnant_button = pregnant_button;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            /*
            tarih önceden ayarlanmışsa,
             */
            long get_miliseconds_to_date    = MotherActivity.preferences.getLong(MotherActivity.USER_DUE_DATE_MS,0);
            final Calendar c = Calendar.getInstance();
            if(get_miliseconds_to_date != 0 ) {
                c.setTimeInMillis(get_miliseconds_to_date);
            }
            // Use the current date as the default date in the picker
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);

            dialog.setButton(
                    DialogInterface.BUTTON_NEGATIVE, "Cancel",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.dismiss();
                        Log.d("Button_value","" + which + " ---- " + " negatif");
                    }
                }
            });

            dialog.setButton(
                    DialogInterface.BUTTON_POSITIVE,"Set",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Log.d("Button_value","" + which + " ---- " + " Pozitif");
                        }
                    }
            );

            // Create a new instance of DatePickerDialog and return it
            return dialog;
        }

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            pregnant_button.setText(dayOfMonth + " " + MotherActivity.turkish_monts[monthOfYear] + " " + year);
            long miliseconds_date       = date_to_miliseconds(year, monthOfYear, dayOfMonth);

            SharedPreferences.Editor  editor = MotherActivity.preferences.edit();
            editor.putLong(MotherActivity.USER_DUE_DATE_MS, miliseconds_date);
            editor.apply();
        }

        private long date_to_miliseconds(int year, int month, int day){
            Calendar calendar = Calendar.getInstance();

            calendar.set(year,month,day,00,00);
            return calendar.getTimeInMillis();
        }
    }

帮帮我。 谢谢。

1 个答案:

答案 0 :(得分:2)

我很容易解决问题。

我只添加了一个行代码而不是布尔变量。

  private Button pregnant_button;
        //add this line default = false
        private boolean printer     = false;

        public CalendarItem(Button pregnant_button) {
            this.pregnant_button = pregnant_button;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            /*
            tarih önceden ayarlanmışsa,
             */
            long get_miliseconds_to_date    = MotherActivity.preferences.getLong(MotherActivity.USER_DUE_DATE_MS,0);
            final Calendar c = Calendar.getInstance();
            if(get_miliseconds_to_date != 0 ) {
                c.setTimeInMillis(get_miliseconds_to_date);
            }
            // Use the current date as the default date in the picker
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);


            dialog.setButton(
                    DialogInterface.BUTTON_NEGATIVE, "İptal",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            if (which == DialogInterface.BUTTON_NEGATIVE) {

                            }
                        }
                    });
            dialog.setButton(
                    DialogInterface.BUTTON_POSITIVE, "Tamam",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            if (which == DialogInterface.BUTTON_POSITIVE) {
                                printer = true;
                            }
                        }
                    });

            // Create a new instance of DatePickerDialog and return it
            return dialog;
        }

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

            if( printer ) {
                pregnant_button.setText(dayOfMonth + " " + MotherActivity.turkish_monts[monthOfYear] + " " + year);
                long miliseconds_date       = date_to_miliseconds(year, monthOfYear, dayOfMonth);
                SharedPreferences.Editor  editor = MotherActivity.preferences.edit();
                editor.putLong(MotherActivity.USER_DUE_DATE_MS, miliseconds_date);
                editor.apply();
            }
        }