Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Expanding Adam's option into an slightly lighter weight interpretation and potentially more useful, I decided to maintain an int reference for the element ID that instantiated the dialog request and then just referenced that in the final event handler. This has the added benefit of fitting nicely into a switch statement in this method in case you have multiple date inputs but want specific formatting for each or groups of each. All snippets below are in my Activity class directly</p> <h2>Instance variables</h2> <pre><code>private static final int DIALOG_DATE_PICKER = 100; private int datePickerInput; </code></pre> <h2>Dialog Handler</h2> <pre><code>@Override public Dialog onCreateDialog(int id) { switch(id) { case DIALOG_DATE_PICKER: final Calendar c = Calendar.getInstance(); DatePickerDialog dialog = new DatePickerDialog(this, dateSetListener, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)); return dialog; } return null; } </code></pre> <h2>Click Listener</h2> <pre><code>private OnClickListener datePickerListener = new OnClickListener() { @Override public void onClick(View v) { datePickerInput = v.getId(); showDialog(DIALOG_DATE_PICKER); } }; </code></pre> <h2>Date Selection Handler</h2> <pre><code>private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { switch( datePickerInput ) { case R.id.datePicker1: ((EditText) findViewById( datePickerInput )) .setText(...); ... break; case R.id.datePicker2: ... break; default: ... break; } } }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload