Android DatePicker 调用两次后 TextView 更新

TextView updates after Android DatePicker call two times

我是 android 的新手,我想在单击时甚至在 TextView 上显示对话框日期选择器。但是,从文本视图上的日期选择器设置日期需要两次。

TextView XML:

  <TextView
    android:id="@+id/dateTxtView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="MM/DD/YYYY">

Activity代码:

private static final String TAG = "MainActivity";
private TextView mDisplayDate;
private DatePickerDialog.OnDateSetListener mDateSetListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Setting date at the start of the app
     mDisplayDate = (TextView) findViewById(R.id.dateTxtView);

            mDisplayDate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Calendar mCalendar = Calendar.getInstance();
                    int year = mCalendar.get(Calendar.YEAR);
                    int month = mCalendar.get(Calendar.MONTH);
                    int day = mCalendar.get(Calendar.DAY_OF_MONTH);
                    DatePickerDialog dateDialog = new DatePickerDialog(
                            MainActivity.this,
                            android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                            mDateSetListener,
                            year,month,day
                    );
                    dateDialog.setTitle("Select the date");
                    dateDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    dateDialog.show();
                    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                            month = month + 1;
                            Log.d(TAG,"date has been set");
                            String dateToday = month + "/" + dayOfMonth + "/" + year;
                            mDisplayDate.setText(dateToday);
                        }
                    };

                }
            });

首先,我单击文本视图并显示对话框日期选择器,在设置日期并单击确定后,没有值传递到文本视图。第二次,我再次点击并设置另一个日期,日期现在传递给 textview。请帮我解决问题。

您应该声明并定义您的侦听器

                mDateSetListener = new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        month = month + 1;
                        Log.d(TAG,"date has been set");
                        String dateToday = month + "/" + dayOfMonth + "/" + year;
                        mDisplayDate.setText(dateToday);
                    }
                };

在选择器构造函数中使用它之前

DatePickerDialog dateDialog = new DatePickerDialog(
                        MainActivity.this,
                        android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                        mDateSetListener,
                        year,month,day
                );
   Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);

mDateSetListener = (datePicker, year1, month1, day1) -> {
            cal.set(year1, month1, day1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd-yyyy", Locale.getDefault());
            ((IconButtonPlaceHolder) mRelatedView).setText(simpleDateFormat.format(cal.getTime()));
        };

        DatePickerDialog dialog = new DatePickerDialog(
                activity,
                android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                mDateSetListener,
                year, month, day);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.show();