DatePickerDialog加载在Fragment中,今天显示为首次打开对话框时的默认日期。然后,用户选择日期,然后关闭对话框。在下次重新打开时,如何将之前选择的日期显示为默认日期而不是显示今天的日期?
我在Activity中尝试过.set()但不知道如何使用.get()来检索片段中的日期。我已经在Activity和fragement之间尝试了一个带有监听器的接口,但是无法让它工作,对于我想要实现的简单任务来说,它似乎过于复杂。我已经尝试了从活动发送到片段的捆绑包但我无法让它工作。请指教。
活动文件:
...
final Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
片段文件:
...
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@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);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Save selected date in a date variable here? How?
// How do I then use the date variable in onCreateDialog
// rather than the get(Calendar) code that returns today's date?
}
}
答案 0 :(得分:1)
获取今天默认日期的DatePicker:
{{1}}
答案 1 :(得分:1)
在Bundle中,您应该发送年月和日值并在此处进行检索,如:
@Override
public Dialog onCreateDialog(Bundle mBundle) {
int year = mBundle.getInt("YEAR");
int month = mBundle.getInt("MONTH");
int day = mBundle.getInt("DAY");
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Save selected date in a date variable here? How?
// How do I then use the date variable in onCreateDialog
// rather than the get(Calendar) code that returns today's date?
}
我们从那里调用此日期时间选择器片段,您需要发送此包。
答案 2 :(得分:1)
全球宣布
this.selectedHero = {...hero};
let {...example} = hero;
复制并粘贴功能
private DatePicker datepicker;
答案 3 :(得分:0)
如果我关注您的问题,请将日期放在sharedPreferences
上
public void onDateSet(DatePicker view, int year, int month, int day) {
// here put you date to shared preference in get later//
// you can convert it to time stamp and easily save
}
使用sharedPreferences
,您可以设置任何位置并获取应用中的任何位置。
答案 4 :(得分:0)
请得到这个
private String lastStoredDate;
if(lastStoredDate == null || lastStoredDate.trim().length() == 0){
lastStoredDate = getCurrentDate("MM/dd/yyyy");
}
String dobSplit[] = lastStoredDate.split("/");
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(dobSplit[2]), Integer.parseInt(dobSplit[0]), Integer.parseInt(dobSplit[1]));
int iDay=calendar.get(Calendar.DATE);
int iMonth=calendar.get(Calendar.MONTH)-1;
int iYear=calendar.get(Calendar.YEAR);
DatePickerDialog dbDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
StringBuilder date = new StringBuilder();
monthOfYear = monthOfYear + 1;
if (monthOfYear < 10) {
date.append("0"+monthOfYear+"/"+dayOfMonth);
}else{
date.append(monthOfYear).append("/"+dayOfMonth);
}
date.append("/"+year);
lastStoredDate = date.toString();
}
},iYear,iMonth,iDay);
dbDialog.getDatePicker().setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
dbDialog.show();
//获得好日期
public static String getCurrentDate(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
String formattedDate = sdf.format(Calendar.getInstance().getTime());
return formattedDate;
}
答案 5 :(得分:0)
First of all this code is working on `edittext` or any button to set previous and current date in to Date picker.
public class AboutMeActivity extends AppCompatActivity {
DatePickerDialog datePickerDialog;
private TextInputEditText edtDateOfBirth;
private int mYear;
private int mMonth;
private int mDay;
private View.OnClickListener edtDateOfBirthClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
datePickerDialog = new DatePickerDialog(AboutMeActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
//here i=year,i1= month,i2=days.
mYear =i;
mMonth=i1;
mDay=i2;
edtDateOfBirth.setText(i2 + "-" + (i1 + 1) + "-" + i);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
edtDateOfBirth = (TextInputEditText)findViewById(R.id.edtDateOfBirth); if(edtDateOfBirth!=null)edtDateOfBirth.setOnClickListener(edtDateOfBirthClickListener);
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR); // current year
mMonth = c.get(Calendar.MONTH); // current month
mDay = c.get(Calendar.DAY_OF_MONTH); //current Day.
}
}
答案 6 :(得分:0)
我的“编辑”文本字段显示当前日期,并在下次打开“日期选择器”对话框时更新最后选择的日期,方法是从编辑文本字段中获取值并更新“日期选择器”对话框。
我对在“编辑”文本字段中设置的“点击”功能有非常详细的解释。
我已经发布了整个功能,您可以从评论中看到哪些部分可以解决您的问题
注意:在默认情况下,在编辑文本视图中显示当前日期时,我没有捕获空值。
public void setDateDialog(View view) {
// Edit text field which on click opens the Datepicker Dialog
final EditText eText = findViewById(R.id.timeField);
// Get the current value in the edit text field
String userInput = eText.getText().toString();
// Remove the date seprators
String[] dateParts = userInput.split("/");
// Day String
String dayStr = dateParts[0];
// Month String
String monthStr = dateParts[1];
// Year String
String yearStr = dateParts[2];
// Day String to integer
final int dayInt = Integer.parseInt(dayStr);
// Month String to integer ( Substarct 1 as month as count starts from 0)
final int monthInt = Integer.parseInt(monthStr)-1;
// Year String to integer
final int yearInt = Integer.parseInt(yearStr);
// Initialise the calender
final Calendar cldr = Calendar.getInstance();
int day = cldr.get(Calendar.DATE);
int month = cldr.get(Calendar.MONTH);
int year = cldr.get(Calendar.YEAR);
DatePickerDialog pickerDialog = new DatePickerDialog(QuickLaunch.this, R.style.DatePickerTheme, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Adding Zero before month if month is single digit
int month = monthOfYear + 1;
String fm = "" + month;
String fd = "" + dayOfMonth;
if (month < 10) {
fm = "0" + month;
}
if (dayOfMonth < 10) {
fd = "0" + dayOfMonth;
}
eText.setText(fd + "/" + (fm) + "/" + year);
}
}, year, month, day);
// Set the maximum date till tommorrow
pickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + (1000 * 60 * 60 * 24 * 1));
// Set the minimun date till yesterday
pickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - (1000 * 60 * 60 * 24 * 1));
//Set the previous date in datepicker
pickerDialog.getDatePicker().init(yearInt,monthInt,dayInt,null);
pickerDialog.show();
}