1

Le problème auquel je suis confronté concerne la boîte de dialogue de la date et de l'heure. J'ai créé chacun d'eux séparément. Sur onCreate, j'ai défini la date et l'heure actuelles dans la boîte de dialogue de l'utilisateur. par exemple pour 24/7/2012 comme date d'aujourd'hui. Chaque fois que je change/cliquez sur la boîte de dialogue pour le date de départ à la date par exemple: 26/7/2012 puis j'ai changé le texte de la boîte de dialogue to_date à 26/7/2012 via setText. Le problème qui se pose ici est lorsque j'ouvre la boîte de dialogue to_date la date qui apparaît dans la boîte de dialogue est 24/7/2012 et non 26/7/2012. Comment cela peut-il être réalisé pour la boîte de dialogue du sélecteur de date et d'heure. J'ai joint le code ci-dessous. Merci d'avance.Problème de datepicker et de timepicker Android

public Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DATE_DIALOG_ID_FROM: 
     Log.i("Date Case", "" + from_day); 
     return new DatePickerDialog(this, dateListenerfrom, from_year, 
       from_month, from_day); 
    case DATE_DIALOG_ID_TO: 
     Log.i("Date Case", "" + to_day); 
     return new DatePickerDialog(this, dateListenerto, to_year, 
       to_month, to_day); 
    case TIME_DIALOG_ID_FROM: 
     return new TimePickerDialog(this, timeListenerfrom, from_hours, 
       from_min, false); 
    case TIME_DIALOG_ID_TO: 
     return new TimePickerDialog(this, timeListenerto, to_hours, to_min, 
       false); 

    } 
    return null; 

} 

private DatePickerDialog.OnDateSetListener dateListenerfrom = new DatePickerDialog.OnDateSetListener() { 

    public void onDateSet(DatePicker view, int yr, int monthOfYear, 
      int dayOfMonth) { 
     // TODO Auto-generated method stub 

     from_year = yr; 
     from_month = monthOfYear; 
     from_day = dayOfMonth; 
     // to_year = yr; 
     // to_month = monthOfYear; 
     to_day = dayOfMonth; 

     Log.i("From Day", "" + from_day); 
     Log.i("To Day", "" + to_day); 
     Log.i("To Cons", "" + dayOfMonth); 

     try { 
      updateDateFrom(); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
}; 

private DatePickerDialog.OnCancelListener mDateFromCancelListener = new DatePickerDialog.OnCancelListener() { 
    public void onCancel(DialogInterface dialog) { 

    } 
}; 

private DatePickerDialog.OnDateSetListener dateListenerto = new DatePickerDialog.OnDateSetListener() { 

    public void onDateSet(DatePicker view, int yr, int monthOfYear, 
      int dayOfMonth) { 
     // TODO Auto-generated method stub 
     to_year = yr; 
     to_month = monthOfYear; 
     to_day = dayOfMonth; 

     Log.i("From Day", "" + from_day); 
     Log.i("To Day", "" + to_day); 
     Log.i("To Cons", "" + dayOfMonth); 

     try { 
      updateDateTo(); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
}; 

private void updateDateFrom() throws ParseException { 

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); 

    try { 
     date_from = dateFormat.parse(from_day + "/" + from_month + "/" 
       + from_year + " " + from_hours + ":" + to_min); 

    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    System.out.println("From LONG " + date_from.getTime()); 

    from_date_builder = new StringBuilder().append(from_day).append('/') 
      .append(from_month + 1).append('/').append(from_year); 

    from_date.setText(from_date_builder.toString()); 

    to_date.setText(new StringBuilder().append(from_day).append('/') 
      .append(from_month + 1).append('/').append(from_year)); 

} 

private void updateDateTo() throws ParseException { 

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); 

    try { 
     date_to = dateFormat.parse(to_day + "/" + to_month + "/" + to_year 
       + " " + to_hours + ":" + to_min); 

    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    System.out.println("To LONG " + date_to.getTime()); 

    if (date_from.getTime() <= date_to.getTime()) { 
     to_date_builder = new StringBuilder().append(to_day).append('/') 
       .append(to_month + 1).append('/').append(to_year); 
     to_date.setText(to_date_builder.toString()); 
    } else { 
     Toast.makeText(this, "Please set proper date", Toast.LENGTH_SHORT) 
       .show(); 
    } 

} 

private TimePickerDialog.OnTimeSetListener timeListenerfrom = new TimePickerDialog.OnTimeSetListener() { 
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     from_hours = hourOfDay; 
     from_min = minute; 
     try { 
      updateTimeFrom(); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
}; 

private TimePickerDialog.OnTimeSetListener timeListenerto = new TimePickerDialog.OnTimeSetListener() { 
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     to_hours = hourOfDay; 
     to_min = minute; 
     try { 
      updateTimeTo(); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
}; 

private void updateTimeFrom() throws ParseException { 

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); 

    time_from = dateFormat.parse(from_day + "/" + from_month + "/" 
      + from_year + " " + from_hours + ":" + from_min); 

    from_time_builder = new StringBuilder().append(from_hours).append(':') 
      .append(from_min); 

    from_time.setText(from_time_builder.toString()); 

    to_time.setText(new StringBuilder().append(from_hours).append(':') 
      .append(from_min)); 

} 

private void updateTimeTo() throws ParseException { 

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); 

    time_to = dateFormat.parse(to_day + "/" + to_month + "/" + to_year 
      + " " + to_hours + ":" + to_min); 

    if (time_from.getTime() <= time_to.getTime() 
      && date_from.getTime() <= date_to.getTime()) { 

     to_time_builder = new StringBuilder().append(to_hours).append(':') 
       .append(to_min); 

     to_time.setText(to_time_builder.toString()); 
    } 

    else { 
     Toast.makeText(this, "Please set proper date", Toast.LENGTH_SHORT) 
       .show(); 
    } 

    System.out.println("Thi is in the time to "); 

} 

Ce onclick {

if (v == from_date) { 
     showDialog(DATE_DIALOG_ID_FROM); 
    } 

    if (v == to_date) { 
     showDialog(DATE_DIALOG_ID_TO); 
    } 

    if (v == from_time) { 
     showDialog(TIME_DIALOG_ID_FROM); 
    } 
    if (v == to_time) { 
     showDialog(TIME_DIALOG_ID_TO); 
    } 

}

Répondre

1

vous devez implémenter la méthode ci-dessous dans votre activité pour mettre à jour la date/heure à afficher dans la boîte de dialogue.

protected void onPrepareDialog(int id, Dialog dialog) { 
    switch (id) { 
     case DATE_DIALOG_ID_TO: 
     case DATE_DIALOG_ID_FROM: 
      ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); 
      break; 
    } 
} 
+0

Salut Sudhaker, Cela a fonctionné dans une certaine mesure grâce, plus tard, j'ai fait la validation lorsque dans le to_date est inférieur à la from_date la date ne sera pas définie. Cela jette un toast, puis de nouveau quand j'ouvre la boîte de dialogue to_date, la date est la date validée incorrecte et pas la précédente sur ce qui a été défini via settext sur le bouton. – dominic

0

Si vous voulez montrer from_date par défaut pour to_date, vous pouvez changer votre to_date datepicker pour afficher des valeurs de date.

case DATE_DIALOG_ID_TO: 
    Log.i("Date Case", "" + to_day); 
    return new DatePickerDialog(this, dateListenerto, from_year, 
      from_month, from_day); 

Ou, vous pouvez définir to_year, to_month et to_day de la fonction onDateSet du sélecteur from_date.

+0

Je l'ai essayé, après le réglage via settext il contient la date précédente. – dominic

Questions connexes