【问题标题】:DatePickerDialog resets the date in AndroidDatePickerDialog 重置 Android 中的日期
【发布时间】:2023-03-07 06:46:01
【问题描述】:

我在我的应用程序中使用datePickerDialog。我已将 当前日期onClickListener 都设置在同一个 textView

最初 textView 显示当前日期,当它监听点击事件时,它显示一个DatePickerDialog

我的问题是什么?

当我点击textView 时,它会显示带有当前日期DatePicker,然后我设置任何日期,新日期现在在 textView 上可见 每次我点击 textView 它在datePickerDialog 中显示当前日期,我希望datepicker更新 datePickerDialog 上的日期。

这是我的Activity.javasn-p

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

//format of the current date
String currDate = new SimpleDateFormat("MM/dd/yyyy").format(new Date());

//set current date on textView
dateView = (TextView) findViewById(R.id.date_view);
dateView.setText(currDate);

dateView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        DatePickerDialog dpd = new DatePickerDialog(TRTimeReminder.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        c.set(year, month, day);
                        String date = new SimpleDateFormat("MM/dd/yyyy").format(c.getTime());
                        dateView.setText(date);
                    }
                }, mYear, mMonth, mDay);
            dpd.show();
        }
    });

编辑

我还将DatePickerDialog(TRTimeReminder.this,.....,mYear, mMonth, mDay) 更新为DatePickerDialog(TRTimeReminder.this,....., y, m, d)

其中int y, m, d 是我在onDateSet() 方法中定义为y = year, m = month, d = day 的变量,但是当我点击textView 时它显示初始化日期JAN 1 1900

【问题讨论】:

    标签: java android date datepicker


    【解决方案1】:

    据我所知,您正在使用 mYear, mMonth, mDay 初始化 dpd,而您永远不会更新这些内容。

    关于评论的更新:

    在初始化新的DatePickerDialog 时从Calendar c 中提取数据,而不是使用存储的值。

    DatePickerDialog dpd = new DatePickerDialog(TRTimeReminder.this,
      new DatePickerDialog.OnDateSetListener() {
            @Override
            Public void onDateSet(DatePicker view, int year, int month, int day) {
            c.set(year, month, day);
            String date = new SimpleDateFormat("MM/dd/yyyy").format(c.getTime());
            dateView.setText(date);
            }
    
            c.get(Calender.YEAR); // I forgot if you need the ; here when using Java
            c.get(Calender.MONTH;
            c.get(Calender.DAY_OF_MONTH);
    }); 
    

    【讨论】:

    • 我也试过改变它。我取了int y, m, d 并在onDateSet 中定义了变量,即y = year m = month d = date 然后放入y, m, d 而不是mYear, mMonth, mDay。但是当我点击textView 选择一个日期时,它会将日期初始化为 JAN 1 1900
    • 不确定我是否理解您描述的更改。据我所知,您的问题是您从日历 c 中取出日期并存储它,当您更新 c 中的日期时,您永远不会更改这些存储的值,然后当您打开新的 DatePickerDialog 时,您使用旧值.为什么不用 c.get(Calendar.YEAR) 初始化;而不是我的年?
    • 请您在回答中解释一下
    【解决方案2】:

    我认为您的代码没有优化或正确有几个原因:

    1. 每个点击事件都有一个新的日期选择器实例。
    2. 我认为您甚至都没有放弃以前的日期选择器,即使这样,这段代码的状态也很糟糕。
    3. 设置当前日期后,您永远不会使用新值重新初始化成员变量 mYear、mMonth、mDay。

    修复你的代码 -

    DatePickerDialog dpd = new DatePickerDialog(TRTimeReminder.this,
      new DatePickerDialog.OnDateSetListener() {
            @Override
            Public void onDateSet(DatePicker view, int year, int month, int day) {
                mYear = year;
                mMonth = month;
                mDay = day;
               // Perform your operation
            }
    
    }, mYear, mMonth, mDay);
    

    根据我的建议,我更喜欢实现以下代码:-

    public class CustomDatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    
        private static final String CUSTOM_DIALOG_TAG="custom_dialog";
    
        public static void showDialog(FragmentManager fm){
            FragmentTransaction ft = fm.beginTransaction();
            CustomDatePicker mPrveInstance = (CustomDatePicker)fm.findFragmentByTag(CUSTOM_DIALOG_TAG);
            if (mPrveInstance != null) {
                ft.remove(mPrveInstance);
            }
            ft.addToBackStack(null);
            // Create and show the dialog.
            CustomDatePicker datePicker = new CustomDatePicker();
            datePicker.show(ft, CUSTOM_DIALOG_TAG);
        }
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
    
            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }
    
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            // Create Interface and send callback to activity to call show
            // Avoid exposing dialog fragment instance to activity
            Calendar.getInstance().set(year, month, day);
        }
    
    }
    

    只要你想显示,就直接从Activity调用show方法。

    // CustomDatePicker.showDialog(getFragmentManager());
    

    【讨论】:

    • 我没有使用任何FragmentManager
    • 不使用片段管理器是你的选择,但我更喜欢这样。我已经解释过如果你想使用不要在每次点击回调时创建多个实例。
    【解决方案3】:

    在用户设置日期后调用 OnDateSet。如果要在对话框中显示上一个日期,您必须首先调用 onCreateDialog 并设置日期。请参见下面的代码示例:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
    int year = 0;
    int month = 0;
    int day = 0;
    
    // get previous date if available
    String dateFormatted = dateView.getText();
    Log.i(TAG, "What is the user's date " + dateFormatted);
    
    if (dateFormatted == null || dateFormatted.isEmpty())
    {
      // Use the current date as the default date in the picker
      final Calendar c = Calendar.getInstance();
      year = c.get(Calendar.YEAR);
      Log.i(TAG, "what is the phone's year: " + year);
      month = c.get(Calendar.MONTH);
      Log.i(TAG, "what is the phone's month: " + month);
      day = c.get(Calendar.DAY_OF_MONTH);
      Log.i(TAG, "what is the phone's day: " + day);
    } else
    {
      day = Integer.parseInt(dateFormatted.substring(0, 2));
      Log.i(TAG, "what is the user's day: " + day);
      month = Integer.parseInt(dateFormatted.substring(3, 5));
      // Correct month to 0-11 format from 1-12 format
      month = month - 1;
      Log.i(TAG, "what is the user's month: " + month);
      year = Integer.parseInt(dateFormatted.substring(6, 10));
      Log.i(TAG, "what is the user's year: " + year);
    }
    
    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
    }
    

    注意我正在使用实现 DatePickerDialog.OnDateSetListener 的 DialogFragment。因此,您可能需要将 getActivity 更改为此。我想它会起作用。

    然后使用onDateSet:

    public void onDateSet(DatePicker view, int year, int month, int day)
    {
    // Do something with the date chosen by the user
    Time time = new Time();
    time.set(day, month, year);
    String date = time.format("%d-%m-%Y");
    Log.i(TAG, "The date you are setting: " + date);
    dateView.setText(date);
    Log.i(TAG, "You have set the date to: " + date);
    }
    

    【讨论】:

      【解决方案4】:

      在取消点击时重置日期选择器:

      1. 在日期选择器上添加 onDismissListner
      2. 在 onDismissListener 内部,调用 updateData 方法

      例子:

      datePicker.setOnDismissListener((dialog) -> {
          datePicker.updateData(Year, Month, Date);
      }
      

      注意:您可能会认为是用户单击确定此代码会出现问题。理想情况下不会。当您在 onDateSet 中调用 updateData 时,您不会遇到任何问题。

      参考:https://stackoverflow.com/a/57703547/1994950

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多