DatePickerDialog重置Android中的日期

时间:2015-01-18 17:13:55

标签: java android date datepicker

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

最初textView显示当前日期,当它侦听点击事件时,它会显示DatePickerDialog

我的问题是什么?

当我点按textView时,它显示DatePicker 当前日期,然后我设置了任何日期,现在可以在textView 上看到新日期,但我每次点击textView时都会在datePickerDialog中显示当前日期,我希望datepicker 更新 {{1}上的日期也。

这是我的datePickerDialog代码段

Activity.java

修改

我还将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()的变量,但是当我点击textView时,它会显示初始化日期 JAN 1 1900

4 个答案:

答案 0 :(得分:1)

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

有关评论的更新:

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

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

答案 1 :(得分:1)

由于以下几个原因,我认为您的代码不是优化或正确的:

  1. 每次点击活动都有新的日期选择器实例。
  2. 我认为你甚至不会解雇之前的日期选择器,即使这样,这段代码也很糟糕。
  3. 设置当前日期后,您永远不会使用新值重新初始化成员变量mYear,mMonth,mDay。
  4. 修复您的代码 -

    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());
    

答案 2 :(得分:0)

在用户设置日期后调用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);
}

N.B。我正在使用DialogFragment实现DatePickerDialog.OnDateSetListener。因此,您可能需要将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);
}

答案 3 :(得分:0)

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

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

示例:

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

注意::您可能会认为用户单击“确定”,此代码将产生问题。理想情况下不。在onDateSet中调用updateData时,不会有任何问题。

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