Android 如何更改DatePickerDialog';从今天起的默认日期';将日期更改为用户选择的日期?
我有一个DatePickerDialog,它在一个片段中打开。首次打开时,对话框的默认日期显示今天的日期。然后用户选择一个日期。当对话框关闭时,我用用户选择的日期设置了一个EditText行 下次打开对话框时,我想显示用户以前选择的日期,而不是今天的日期。我是否应该在onDateSet函数中创建一个Bundle来保存所选日期?如果是这样,下次打开对话框时如何使用该捆绑包填充对话框?EditText行上的空白测试是否有用,以便在该行已设置日期的情况下,对话框不会填充今天的日期 部分DatePickerFragment文件:Android 如何更改DatePickerDialog';从今天起的默认日期';将日期更改为用户选择的日期?,android,android-fragments,android-datepicker,Android,Android Fragments,Android Datepicker,我有一个DatePickerDialog,它在一个片段中打开。首次打开时,对话框的默认日期显示今天的日期。然后用户选择一个日期。当对话框关闭时,我用用户选择的日期设置了一个EditText行 下次打开对话框时,我想显示用户以前选择的日期,而不是今天的日期。我是否应该在onDateSet函数中创建一个Bundle来保存所选日期?如果是这样,下次打开对话框时如何使用该捆绑包填充对话框?EditText行上的空白测试是否有用,以便在该行已设置日期的情况下,对话框不会填充今天的日期 部分DatePick
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private int currentyear;
private int currentmonth;
private int currentday;
public DatePickerFragment() {
}
public interface OnDateEnteredListener {
void OnDateEntered(int year, int month, int day);
}
OnDateEnteredListener mListener;
// Set up a Calendar object that will capture the current date for the DatePickerDialog to use.
Calendar cal = Calendar.getInstance();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Bundle datebundle = this.getArguments();
currentyear = datebundle.get(Calendar.YEAR);
currentmonth = datebundle.get(Calendar.MONTH);
currentday = datebundle.get(Calendar.DAY_OF_MONTH);
if(currentyear != 0){
DatePickerDialog dateDialog = new DatePickerDialog(this.getActivity(), this, currentyear, currentmonth, currentday);
dateDialog.getDatePicker().setCalendarViewShown(true);
dateDialog.getDatePicker().setSpinnersShown(false);
dateDialog.setTitle("Select a Due Date");
return dateDialog;
}
else {
// Create three variables to capture the current date.
currentyear = cal.get(Calendar.YEAR);
currentmonth = cal.get(Calendar.MONTH);
currentday = cal.get(Calendar.DAY_OF_MONTH);
// Create a DatePickerDialog which is a simple dialog containing a DatePicker. Use the current date
// as the default date in the DatePicker. The new instance of DatePickerDialog is created by passing
// 5 parameters/arguments to the constructor and returning it.
DatePickerDialog dateDialog = new DatePickerDialog(this.getActivity(), this, currentyear, currentmonth, currentday);
dateDialog.getDatePicker().setCalendarViewShown(true);
dateDialog.getDatePicker().setSpinnersShown(false);
dateDialog.setTitle("Select a Due Date");
return dateDialog;
}
}
public void onDateSet (DatePicker view,int year, int month, int day) {
mListener.OnDateEntered(year,month,day);
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
部分活动文件:
public class Activity extends AppCompatActivity implements DatePickerFragment.OnDateEnteredListener {
int currentyear;
int currentmonth;
int currentday;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
fListenerEditText = (ListenerEditText) findViewById(R.id.FEditText);
fListenerEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && (fListenerEditText.getText().length() == 0)) {
DialogFragment newFragment = new DatePickerFragment();
Bundle datebundle = new Bundle();
datebundle.putInt("YEAR", currentyear);
datebundle.putInt("MONTH", currentmonth);
datebundle.putInt("DAY", currentday);
newFragment.setArguments(datebundle);
newFragment.show(getSupportFragmentManager(), "datePicker");
}
}
});
@Override
public void onDateEntered(int year, int month, int day) {
Toast toast = Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_LONG);
toast.show();
}
}
当用户选择日期时,一旦对话框关闭,使用以下命令将数据传回活动(将数据从片段传回开始活动): 在活动中收到值后,在启动DatePickerFragment时使用以下命令:
DatePickerFragment datepickerfragment = new DatePickFragment();
Bundle bundle = new Bundle();
bundle.putInt("YEAR", currentyear);
bundle.putInt("MONTH", currentmonth);
bundle.putInt("DAY", currentday);
datepickerfragment.Arguments = bundle;
fragmentmanager.BeginTransaction().Add(R.layout.container, datepickerfragment).Commit();
其中currentyear、currentmonth和currentday是用户选择的日历值。下次打开对话框片段时,检索其中的值,如下所示:
int year = this.Arguments.getInt("YEAR");
int month = this.Arguments.getInt("MONTH");
int day = this.Arguments.getInt("DAY");
现在,您可以将对话框的日期设置为从上一个DatePickerFragment的值中获取的年、月、日
编辑:
要设置值,请在onCreateDialog(捆绑包)中执行以下操作:
....
int year = this.Arguments.getInt("YEAR");
int month = this.Arguments.getInt("MONTH");
int day = this.Arguments.getInt("DAY");
if(year != null){
return new DatePickerDialog(getActivity(), this, year, month, day);
}else{
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
当用户选择日期时,一旦对话框关闭,使用以下命令将数据传回活动(将数据从片段传回开始活动): 在活动中收到值后,在启动DatePickerFragment时使用以下命令:
DatePickerFragment datepickerfragment = new DatePickFragment();
Bundle bundle = new Bundle();
bundle.putInt("YEAR", currentyear);
bundle.putInt("MONTH", currentmonth);
bundle.putInt("DAY", currentday);
datepickerfragment.Arguments = bundle;
fragmentmanager.BeginTransaction().Add(R.layout.container, datepickerfragment).Commit();
其中currentyear、currentmonth和currentday是用户选择的日历值。下次打开对话框片段时,检索其中的值,如下所示:
int year = this.Arguments.getInt("YEAR");
int month = this.Arguments.getInt("MONTH");
int day = this.Arguments.getInt("DAY");
现在,您可以将对话框的日期设置为从上一个DatePickerFragment的值中获取的年、月、日
编辑:
要设置值,请在onCreateDialog(捆绑包)中执行以下操作:
....
int year = this.Arguments.getInt("YEAR");
int month = this.Arguments.getInt("MONTH");
int day = this.Arguments.getInt("DAY");
if(year != null){
return new DatePickerDialog(getActivity(), this, year, month, day);
}else{
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
我认为,在创建DatePickerDialog时,应该创建一个新的OnDateSetListener。这样,您将有多个“onDateSet”方法。好的,那么OnDateSetListener将如何在onCreateDialog中使用呢?请检查这一点以获得Pragnani的答案。代码似乎将今天的日期设置为对话框的默认日期。onDateSet中选择的日期似乎未在onCreateDialog中使用。我在这里遗漏了什么吗?我认为,在创建DatePickerDialog时,您应该创建一个新的OnDateSetListener。这样,您将有多个“onDateSet”方法。好的,那么OnDateSetListener将如何在onCreateDialog中使用呢?请检查这一点以获得Pragnani的答案。代码似乎将今天的日期设置为对话框的默认日期。onDateSet中选择的日期似乎未在onCreateDialog中使用。我在这里遗漏了什么吗?好的,那么用户选择的日历值是在片段的onDateSet方法中设置的还是在onCreateDialog中设置的?@AJW它们是在onCreateDialog中设置的。好的,那么我如何在onCreateDialog中在其他三个“当前”之间写入选择捕获今天日期和用户选择日期的三个变量?因此,如果有以前选择的日期,则年份将不为空,因此使用Bundle args创建对话框。如果没有年份,那么这是第一次创建对话框,所以只需使用今天的日期即可。对吗?@AJW对。这就是我们想要的。好的,那么用户选择的日历值是在片段的onDateSet方法中设置的还是在onCreateDialog中设置的?@AJW它们是在onCreateDialog中设置的。好的,那么我如何在onCreateDialog中在其他三个“当前”之间写入选择呢捕获今天日期和用户选择日期的三个变量?因此,如果有以前选择的日期,则年份将不为空,因此使用Bundle args创建对话框。如果没有年份,那么这是第一次创建对话框,所以只需使用今天的日期即可。对吗?@AJW对。这就是我们的初衷。