Android DatePickerDialog额外/冗余调用

时间:2018-04-18 08:02:24

标签: android datepicker

我有DatePickerDialog功能的简单问题。我点击了一个编辑文字视图,点击时弹出DatePickerDialog,如下所示,

        mNoteDateTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar c = Calendar.getInstance();
            currentYear = c.get(Calendar.YEAR);
            currentMonth = c.get(Calendar.MONTH);
            currentDay = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog datePickerDialog = new DatePickerDialog(NewNoteActivity.this,
                    new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {

                    yearFinal = i;
                    monthFinal = i1 + 1;
                    dayFinal = i2;

                    Log.v(TAG, "\n" + " year: " + yearFinal + "\n" +
                            "Month:" + monthFinal + "\n" +
                            "Day:" + dayFinal);
                }
            }, currentYear, currentMonth, currentDay);

            datePickerDialog.show();
        }
    });

问题是,在我选择日期并单击对话框中的“完成”按钮后,我看到Log.v()输出两次,

04-18 00:50:03.832 15601-15601/myexample.com.notes V/MyActivity:  year: 2018
Month:5
Day:19
04-18 00:50:03.832 15601-15601/myexample.com.notes V/MyActivity:  year: 2018
Month:5
Day:19

我无法弄清楚为什么会出现双倍输出?而不是一次?

1 个答案:

答案 0 :(得分:0)

我试过这个只显示一次日志.. 点击按钮

  final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);


            DatePickerDialog datePickerDialog = new DatePickerDialog(Demo.this,
                    new DatePickerDialog.OnDateSetListener() {

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

                            mTvDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                            Log.d("Date To",mTvDate.getText().toString());
                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
            //below line disable old date.
            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);