【发布时间】:2013-05-16 22:35:37
【问题描述】:
所有文档和示例似乎都与“如何从活动中的日期选择器对话框中选择日期”有关。几乎所有这些都将当前日期设置为日期选择器对话框中的默认日期。但是如何传递一个特定的日期,以便当日期选择器打开时,它显示该日期而不是当前日期?
我面临的问题是,当用户单击按钮并且日期选择器对话框第一次打开时,它会显示当前时间。
用户更改值并点击完成。
再次单击该按钮时,将打开日期选择器对话框。但是,该值仍然是默认日期,而不是更改的日期!
代码:
public class DatePickerFragment extends SherlockDialogFragment
implements android.app.DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//TODO Use current date is date null, else show currently displayed date
// 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) {
((NewTransactionActivity) getActivity()).updateDate(year, monthOfYear, dayOfMonth);
}
}
上面的代码是日期选择器总是显示默认日期的原因。但是如何将日期值传递给这个片段?
这是来自活动的代码:
public void showDatePickerDialog(View v) {
Bundle currentDate = new Bundle();
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
【问题讨论】:
标签: android android-activity datepicker