How to handle date picker dialog not to set in edi

2019-08-09 17:13发布

When I click edittext, date picker dialog opens. without clicking done button in date picker and clicked outside of dialog, it sets in edittext.

I checked this but this works for first time. When selecting second time by choosing dialog and without clicking done button, it fails.

private final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
       private boolean fired;
         public void resetFired(){
                fired = false;
            }
            @Override
            public void onDateSet(DatePicker view, int selectedYear,
                    int selectedMonth, int selectedDay) {
                if (fired) {
                    Log.e("DatePicker", "Ignoring for first time");
                    birthDayValue = selectedDay;
                    birthMonthValue = selectedMonth;
                    birthYearValue = selectedYear;
                    String birthDayDateFormt = Utils.getBirthDayDate(selectedYear,
                            selectedMonth, selectedDay);
                    dateOfBirth.setText(birthDayDateFormt);
                    dateOfBirth.setError(null);
                    return;//ignore and return.
                } 
                fired = true;//first time fired
            }
    };

};

How to solve this issue?

1条回答
甜甜的少女心
2楼-- · 2019-08-09 17:56
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
        if(view.isShown()) {
            if (fired) {
                Log.e("DatePicker", "Ignoring for first time");
                birthDayValue = selectedDay;
                birthMonthValue = selectedMonth;
                birthYearValue = selectedYear;
                String birthDayDateFormt = Utils.getBirthDayDate(selectedYear,
                        selectedMonth, selectedDay);
                dateOfBirth.setText(birthDayDateFormt);
                dateOfBirth.setError(null);
                return;//ignore and return.
            } 
            fired = true;//first time fired
          }
        }

if(view.isShown()) add code in this condition, its a simple step do it and check output

查看更多
登录 后发表回答