如何处理日期选择器对话框,在对话框android之外点击时不要在edittext中设置?

时间:2015-02-01 11:58:49

标签: android android-datepicker android-date

单击edittext时,将打开日期选择器对话框。如果没有在日期选择器中单击完成按钮并在对话框外单击,则会在edittext中设置。

checked this但这是第一次有效。通过选择对话框而不单击“完成”按钮来选择第二次时,它会失败。

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
            }
    };

};

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

        @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())在此条件下添加代码, 这是一个简单的步骤并检查输出