DatePickerDialog 重置 Android 中的日期
DatePickerDialog resets the date in Android
我在我的应用程序中使用 datePickerDialog
。我已将 当前日期 和 onClickListener
都设置在同一个 textView
最初 textView 显示当前日期,当它监听点击事件时,它显示一个 DatePickerDialog
我的问题是什么?
当我点击 textView
时,它显示 DatePicker
和 当前日期 ,然后我设置任何日期,新日期现在可以在 textView 但是 每次我点击 textView 它都会在 datePickerDialog
中显示 当前日期 ,我希望 datepicker
到 更新 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()
方法中定义为 y = year, m = month, d = day
的变量,但是当我点击 textView 时它显示初始化日期 JAN 1 1900
据我所知,您正在用 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);
});
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。我正在使用实现 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);
}
出于以下几个原因,我认为您的代码没有优化或正确:
- 每个点击事件都有新的日期选择器实例。
- 我认为您甚至没有解雇以前的日期选择器,即便如此,这段代码的状况也很糟糕。
- 设置当前日期后,您永远不会用新值重新初始化成员变量 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());
要在取消点击时重置日期选择器:
- 在日期选择器上添加 onDismissListner
- 在 onDismissListener 内部,调用 updateData 方法
示例:
datePicker.setOnDismissListener((dialog) -> {
datePicker.updateData(Year, Month, Date);
}
注意:您可能认为是用户点击 Ok 这段代码会出问题。理想情况下不会。当您在 onDateSet 中调用 updateData 时,您不会遇到任何问题。
参考:
我在我的应用程序中使用 datePickerDialog
。我已将 当前日期 和 onClickListener
都设置在同一个 textView
最初 textView 显示当前日期,当它监听点击事件时,它显示一个 DatePickerDialog
我的问题是什么?
当我点击 textView
时,它显示 DatePicker
和 当前日期 ,然后我设置任何日期,新日期现在可以在 textView 但是 每次我点击 textView 它都会在 datePickerDialog
中显示 当前日期 ,我希望 datepicker
到 更新 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()
方法中定义为 y = year, m = month, d = day
的变量,但是当我点击 textView 时它显示初始化日期 JAN 1 1900
据我所知,您正在用 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);
});
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。我正在使用实现 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);
}
出于以下几个原因,我认为您的代码没有优化或正确:
- 每个点击事件都有新的日期选择器实例。
- 我认为您甚至没有解雇以前的日期选择器,即便如此,这段代码的状况也很糟糕。
- 设置当前日期后,您永远不会用新值重新初始化成员变量 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());
要在取消点击时重置日期选择器:
- 在日期选择器上添加 onDismissListner
- 在 onDismissListener 内部,调用 updateData 方法
示例:
datePicker.setOnDismissListener((dialog) -> {
datePicker.updateData(Year, Month, Date);
}
注意:您可能认为是用户点击 Ok 这段代码会出问题。理想情况下不会。当您在 onDateSet 中调用 updateData 时,您不会遇到任何问题。
参考: