如何让 Interface Bundle 将之前的日期传递给 DatePickerDialog?
How do I get Interface Bundle to deliver previous date to DatePickerDialog?
我有一个在片段中打开的 DatePickerDialog。首次打开时,对话框的默认日期显示今天的日期。然后用户选择一个日期。当对话框关闭时,我设置了一个带有用户选择日期的 EditText 行。
下次打开对话框时(因为用户想要编辑他们之前选择的日期),我想将用户之前选择的日期显示为默认日期,而不是今天的日期。我已经使用 Bundle 设置了一个从片段到 activity 的接口。此外,不确定我在 Fragment 的 OnCreateDialog 中对 "year" 的空值测试是否正常工作。请指教。
部分 DatePickerFragment 文件:
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);
}
部分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();
}
}
如果找不到以前的日期,使用 cal.get()
显示对话框以及今天的日期
用户 select 日期,将 EditText
设置为 select 编辑的日期。
如果找到以前的日期,将文本从 EditText
解析为日期格式,使用 cal.set()
将其设置为当前日期,并使用 cal.get()
显示对话框
您可以使用.matches()
方法检查EditText
是否为空。
例如,
if(txtDate.getText().toString().matches(""))
// txtDate was empty
我有一个在片段中打开的 DatePickerDialog。首次打开时,对话框的默认日期显示今天的日期。然后用户选择一个日期。当对话框关闭时,我设置了一个带有用户选择日期的 EditText 行。
下次打开对话框时(因为用户想要编辑他们之前选择的日期),我想将用户之前选择的日期显示为默认日期,而不是今天的日期。我已经使用 Bundle 设置了一个从片段到 activity 的接口。此外,不确定我在 Fragment 的 OnCreateDialog 中对 "year" 的空值测试是否正常工作。请指教。
部分 DatePickerFragment 文件:
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);
}
部分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();
}
}
如果找不到以前的日期,使用
cal.get()
显示对话框以及今天的日期
用户 select 日期,将
EditText
设置为 select 编辑的日期。如果找到以前的日期,将文本从
EditText
解析为日期格式,使用cal.set()
将其设置为当前日期,并使用cal.get()
显示对话框
您可以使用.matches()
方法检查EditText
是否为空。
例如,
if(txtDate.getText().toString().matches(""))
// txtDate was empty