Multiple DatePickers en la misma actividad

Soy absolutamente nuevo en la plataforma Android y he estado construyendo una aplicación mientras aprendía el proceso de desarrollo.

Actualmente, estoy trabajando en una actividad en la que necesito implementar 2 recolectores de fechas. Una es una "Fecha de inicio" y la otra es una "Fecha de finalización". He estado siguiendo el tutorial de DatePicker en la página de desarrolladores de Android aquí: http://developer.android.com/resources/tutorials/views/hello-datepicker.html

Para un DatePicker, funciona muy bien.

Ahora mi problema es, cuando replicar todo el proceso de un selector de segunda fecha, que aparece muy bien en el emulador, así como en el teléfono. Pero cuando no importa qué botón pulse para seleccionar las fechas, sólo se actualiza el primer TextView y el segundo TextView sigue mostrando la fecha actual.

Aquí está el código:

package com.datepicker; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; public class datepicker extends Activity { private TextView mDateDisplay; private TextView endDateDisplay; private Button mPickDate; private Button endPickDate; private int mYear; private int mMonth; private int mDay; static final int START_DATE_DIALOG_ID = 0; static final int END_DATE_DIALOG_ID = 0; /** Called when the activity is first created. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* capture our View elements for the start date function */ mDateDisplay = (TextView) findViewById(R.id.startdateDisplay); mPickDate = (Button) findViewById(R.id.startpickDate); /* add a click listener to the button */ mPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(START_DATE_DIALOG_ID); } }); /* get the current date */ final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); /* display the current date (this method is below) */ updateStartDisplay(); /* capture our View elements for the end date function */ endDateDisplay = (TextView) findViewById(R.id.enddateDisplay); endPickDate = (Button) findViewById(R.id.endpickDate); /* add a click listener to the button */ endPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(END_DATE_DIALOG_ID); } }); /* get the current date */ final Calendar c1 = Calendar.getInstance(); mYear = c1.get(Calendar.YEAR); mMonth = c1.get(Calendar.MONTH); mDay = c1.get(Calendar.DAY_OF_MONTH); /* display the current date (this method is below) */ updateEndDisplay(); } private void updateEndDisplay() { endDateDisplay.setText( new StringBuilder() // Month is 0 based so add 1 .append(mMonth + 1).append("-") .append(mDay).append("-") .append(mYear).append(" ")); } private void updateStartDisplay() { mDateDisplay.setText( new StringBuilder() // Month is 0 based so add 1 .append(mMonth + 1).append("-") .append(mDay).append("-") .append(mYear).append(" ")); } 

/ * La devolución de llamada recibida cuando el usuario "fija" la fecha en el diálogo para la función de fecha de inicio * /

  private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; updateStartDisplay(); } }; @Override protected Dialog onCreateDialog(int id) { switch (id) { case START_DATE_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); } return null; } /* the callback received when the user "sets" the date in the dialog for the end date function */ private DatePickerDialog.OnDateSetListener endDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; updateStartDisplay(); } }; protected Dialog onCreateDialog1(int id) { switch (id) { case END_DATE_DIALOG_ID: return new DatePickerDialog(this, endDateSetListener, mYear, mMonth, mDay); } return null; } 

}

Por favor avise sobre los cambios requeridos para el código.

Necesita hacer 2 diálogos separados de DatePicker

Haz 2 oyentes

 int from_year, from_month, from_day,to_year, to_month, to_day; //initialize them to current date in onStart()/onCreate() DatePickerDialog.OnDateSetListener from_dateListener,to_dateListener; 

Implementarlos …

  from_dateListener = new OnDateSetListener(){ public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) { ... } } }; to_dateListener = new OnDateSetListener(){ public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) { ..... } }; 

Crear diálogos separados para ambos

 int DATE_PICKER_TO = 0; int DATE_PICKER_FROM = 1; @Override protected Dialog onCreateDialog(int id) { switch(id){ case DATE_PICKER_FROM: return new DatePickerDialog(this, from_dateListener, from_year, from_month, from_day); case DATE_PICKER_TO: return new DatePickerDialog(this, to_dateListener, to_year, to_month, to_day); } return null; } 

Tengo una solución que permite un número ilimitado de campos de fecha sin añadir nuevos tipos de diálogo. Cuando el usuario hace clic en uno de los botones, me registro que TextView y Calendario está siendo modificado antes de iniciar el DatePickerDialog. El OnDateSetListener del diálogo entonces actualiza el TextView y el Calendario registrados.

 import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.DatePickerDialog.OnDateSetListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; public class MultiDatePickerActivity extends Activity { private TextView startDateDisplay; private TextView endDateDisplay; private Button startPickDate; private Button endPickDate; private Calendar startDate; private Calendar endDate; static final int DATE_DIALOG_ID = 0; private TextView activeDateDisplay; private Calendar activeDate; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.multidatepicker); /* capture our View elements for the start date function */ startDateDisplay = (TextView) findViewById(R.id.startDateDisplay); startPickDate = (Button) findViewById(R.id.startPickDate); /* get the current date */ startDate = Calendar.getInstance(); /* add a click listener to the button */ startPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDateDialog(startDateDisplay, startDate); } }); /* capture our View elements for the end date function */ endDateDisplay = (TextView) findViewById(R.id.endDateDisplay); endPickDate = (Button) findViewById(R.id.endPickDate); /* get the current date */ endDate = Calendar.getInstance(); /* add a click listener to the button */ endPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDateDialog(endDateDisplay, endDate); } }); /* display the current date (this method is below) */ updateDisplay(startDateDisplay, startDate); updateDisplay(endDateDisplay, endDate); } private void updateDisplay(TextView dateDisplay, Calendar date) { dateDisplay.setText( new StringBuilder() // Month is 0 based so add 1 .append(date.get(Calendar.MONTH) + 1).append("-") .append(date.get(Calendar.DAY_OF_MONTH)).append("-") .append(date.get(Calendar.YEAR)).append(" ")); } public void showDateDialog(TextView dateDisplay, Calendar date) { activeDateDisplay = dateDisplay; activeDate = date; showDialog(DATE_DIALOG_ID); } private OnDateSetListener dateSetListener = new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { activeDate.set(Calendar.YEAR, year); activeDate.set(Calendar.MONTH, monthOfYear); activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); updateDisplay(activeDateDisplay, activeDate); unregisterDateDisplay(); } }; private void unregisterDateDisplay() { activeDateDisplay = null; activeDate = null; } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH)); } return null; } @Override protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); switch (id) { case DATE_DIALOG_ID: ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH)); break; } } } 

Este tipo de flexibilidad es útil en una aplicación en la que no sabe cuántos selectores de fechas necesitará hasta el tiempo de ejecución.

Expandiendo la opción de Adán en una interpretación de peso ligeramente más ligero y potencialmente más útil, decidí mantener una referencia int para el ID de elemento que instanció la solicitud de diálogo y, a continuación, sólo se hace referencia a ello en el controlador de eventos final. Esto tiene la ventaja añadida de encajar muy bien en una instrucción switch en este método en caso de que tenga varias entradas de fecha, pero desea formato específico para cada uno o grupos de cada uno. Todos los fragmentos siguientes están en mi clase de actividad directamente

Variables de instancia

 private static final int DIALOG_DATE_PICKER = 100; private int datePickerInput; 

Manejador de diálogo

 @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; } 

Escuchador de clics

 private OnClickListener datePickerListener = new OnClickListener() { @Override public void onClick(View v) { datePickerInput = v.getId(); showDialog(DIALOG_DATE_PICKER); } }; 

Controlador de selección de fecha

 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; } } }; 

Creo que he encontrado una solución más limpia. Es una mezcla entre lo que recomienda Google y los comentarios que leo aquí. En mi caso, incluso funciona cuando se llama desde un fragmento de Viewpager. Básicamente, paso un paquete de argumentos al fragmento de diálogo al llamar al diálogo selector de mi fragmento, tal como se define aquí: Android: Pasar datos (extras) a un fragmento Entonces recupero el valor del paquete en mi clase DialogFragment, es valioso.

Aquí están los dos oyentes de mis botones startDate y endDate, de mi código Fragment:

  mWylStartDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Bundle bundle = new Bundle(); bundle.putInt("DATE",1); DialogFragment newFragment = new DatePickerFragment(); newFragment.setArguments(bundle); newFragment.show(getFragmentManager(), "datePicker"); } }); mWylEndDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Bundle bundle = new Bundle(); bundle.putInt("DATE",2); DialogFragment newFragment = new DatePickerFragment(); newFragment.setArguments(bundle); newFragment.show(getFragmentManager(), "datePicker"); } }); 

Aquí está mi clase de DatePickerFragment

 public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { static final int START_DATE = 1; static final int END_DATE = 2; private int mChosenDate; int cur = 0; @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); Bundle bundle = this.getArguments(); if(bundle != null){ mChosenDate = bundle.getInt("DATE",1); } switch (mChosenDate) { case START_DATE: cur = START_DATE; return new DatePickerDialog(getActivity(), this, year, month, day); case END_DATE: cur = END_DATE; return new DatePickerDialog(getActivity(), this, year, month, day); } return null; } @Override public void onDateSet(DatePicker datePicker, int year, int month, int day) { if(cur == START_DATE){ // set selected date into textview Log.v("Date Début","Date1 : " + new StringBuilder().append(month + 1) .append("-").append(day).append("-").append(year) .append(" ")); } else{ Log.v("Date fin","Date2 : " + new StringBuilder().append(month + 1) .append("-").append(day).append("-").append(year) .append(" ")); } } 

}

Usted puede utilizar simplemente este tipo

 public class MainActivity extends Activity { private TextView startDateDisplay; private TextView endDateDisplay; private Button startPickDate; private Button endPickDate; private Calendar startDate; private Calendar endDate; static final int DATE_DIALOG_ID = 0; private TextView activeDateDisplay; private Calendar activeDate; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* capture our View elements for the start date function */ startDateDisplay = (TextView) findViewById(R.id.startDateDisplay); startPickDate = (Button) findViewById(R.id.startPickDate); /* get the current date */ startDate = Calendar.getInstance(); /* add a click listener to the button */ startPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDateDialog(startDateDisplay, startDate); } }); /* capture our View elements for the end date function */ endDateDisplay = (TextView) findViewById(R.id.endDateDisplay); endPickDate = (Button) findViewById(R.id.endPickDate); /* get the current date */ endDate = Calendar.getInstance(); /* add a click listener to the button */ endPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDateDialog(endDateDisplay, endDate); } }); /* display the current date (this method is below) */ updateDisplay(startDateDisplay, startDate); updateDisplay(endDateDisplay, endDate); } private void updateDisplay(TextView dateDisplay, Calendar date) { dateDisplay.setText( new StringBuilder() // Month is 0 based so add 1 .append(date.get(Calendar.MONTH) + 1).append("-") .append(date.get(Calendar.DAY_OF_MONTH)).append("-") .append(date.get(Calendar.YEAR)).append(" ")); } public void showDateDialog(TextView dateDisplay, Calendar date) { activeDateDisplay = dateDisplay; activeDate = date; showDialog(DATE_DIALOG_ID); } private OnDateSetListener dateSetListener = new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { activeDate.set(Calendar.YEAR, year); activeDate.set(Calendar.MONTH, monthOfYear); activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); updateDisplay(activeDateDisplay, activeDate); unregisterDateDisplay(); } }; private void unregisterDateDisplay() { activeDateDisplay = null; activeDate = null; } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH)); } return null; } @Override protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); switch (id) { case DATE_DIALOG_ID: ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH)); break; } } } 

Sólo puede utilizar el siguiente código

 @Override protected Dialog onCreateDialog(int id) { /* * final Dialog d = new Dialog(this); d.set */ switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener, cmYear, cmMonth, cmDay); } return null; } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { cmYear = year; cmMonth = monthOfYear; cmDay = dayOfMonth; updateDisplay(); } }; private void updateDisplay() { String date_check = "" + new StringBuilder() // Month is 0 based so add 1 .append(cmYear).append("-").append(cmMonth + 1) .append("-").append(cmDay).append(" "); } 

Puede llamar a Dialog en cualquier onclick

 showDialog(DATE_DIALOG_ID); 

Donde DATE_DIALOG_ID se declara como

 static final int DATE_DIALOG_ID = 0; 

Espero que esto sea útil.

Simplemente puede utilizar una variable booleana para determinar la vista desde la que está realizando la llamada.

  • Elemento de ListView Estado de LongClick para el selector
  • Selector de fondo de botón de Android
  • Usando estados personalizados, onCreateDrawableState nunca se llama
  • "Presione y mantenga presionado" en Android necesita cambiar estados (selector XML personalizado) usando onTouchListener
  • ¿Por qué android: listSelector cambia incorrectamente de Actividad a Actividad
  • Selector de fecha en Android
  • Cómo configurar el tono de timbre con RingtoneManager.ACTION_RINGTONE_PICKER?
  • Heredar selector desplegable
  • que muestran la hora actual en TimePicker en android
  • Android GridView Elemento seleccionado Fondo
  • ImageButton en el widget de pantalla de inicio de Android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.