On my view I have a button which when pressed pops up a DatePickerDialog. The poppedup dialog has a "Done" button. When I press that button the selected date is populated into an EditText box.
Now, when I press the back button (), it still populates the date in the EditText. How do I dismiss the dialog without returning any value from it.
This is code I've got-
The activity which invokes the fragment:
public class TransactionActivity extends FragmentActivity implements iRibbonMenuCallback {
.....
public void selectDate(View view) {
// This is the method invoked by the button
SelectDateFragment newFragment = new SelectDateFragment();
newFragment.show(getSupportFragmentManager(), "DatePicker");
}
public void populateSetDate(int year, int month, int day) {
// Puts the date in the EditText box
mEdit = (EditText)findViewById(R.id.DateText);
mEdit.setText(month+"/"+day+"/"+year);
}
}
A fragment for the Dialog (based on this page on Android API guides):
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
// Calls a method on the activity which invokes this fragment
((TransactionActivity)getActivity()).populateSetDate(yy, mm+1, dd);
}
}
There doesn't seem to be a method analogous with onDataSet for handling the case when data was not set.
Is there a way to cancel/dismiss the popped up datepicker without getting it's value?
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…