How to handle date picker dialog not to set in edittext when clicked outside of dialog android?

萝らか妹 提交于 2019-12-24 10:48:33

问题


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:


        @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



来源:https://stackoverflow.com/questions/28262439/how-to-handle-date-picker-dialog-not-to-set-in-edittext-when-clicked-outside-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!