Edit EditText with Dates Selector

0

How can I make it so that when I mark a date in the Date Selector it is added to the EditText?

Code that I use for the Dates Selector:

private void setAlarm(Uri passuri){

    Calendar cal = Calendar.getInstance();
    cal.set(datePicker.getYear(),
            datePicker.getMonth(),
            datePicker.getDayOfMonth(),
            timePicker.getCurrentHour(),
            timePicker.getCurrentMinute(),
            00);

EDIT: @Keops

    private void setAlarm(Uri passuri){

        Calendar cal = Calendar.getInstance();
        cal.set(datePicker.getYear(),
                datePicker.getMonth(),
                datePicker.getDayOfMonth(),
                timePicker.getCurrentHour(),
                timePicker.getCurrentMinute(),
                00);

        String formatoFecha = "d/MM/yyyy";
        DateFormat df = new SimpleDateFormat(formatoFecha);
        String date = df.format(cal.getTime());

        editTextFecha.setText(date);

        String passString = passuri.toString();

        Intent intent = new Intent(getBaseContext(), otrointento.prueba.notif.AlarmReceiver.class);
        intent.putExtra("KEY_TONE_URL", passString);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getBaseContext(),
                RQS_1,
                intent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

    }
}
    
asked by UserNameYo 06.01.2017 в 19:34
source

2 answers

3

You can do this:

private void setAlarm(Uri passuri){

        Calendar cal = Calendar.getInstance();
        cal.set(datePicker.getYear(),
        datePicker.getMonth(),
        datePicker.getDayOfMonth(),
        timePicker.getCurrentHour(),
        timePicker.getCurrentMinute(),
        00);

       String formatoFecha = "EEE, d MMM yyyy, HH:mm";
       DateFormat df = new SimpleDateFormat(formatoFecha);
       String date = df.format(cal.getTime());

       editText.setText(date);
}

To launch your Calendar you must implement the click listener in your EditText:

editText.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        //Aqui invocas a tu calendario 
        invocaCalendario();
    }
});

In your layout, your EditText adds the following line to allow touch

android:focusable="false"

Code for your calendar

    public void invocaCalendario(){
        Calendar mcurrentDate=Calendar.getInstance();
        int anio =mcurrentDate.get(Calendar.YEAR);
        int mes =mcurrentDate.get(Calendar.MONTH);
        int dia=mcurrentDate.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog mDatePicker=new DatePickerDialog(MiActivity.this, new OnDateSetListener() {                  
            public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {

                // Tu código
            }
        },anio, mes, dia);
        mDatePicker.setTitle("Selecciona Fecha");                
        mDatePicker.show();  }

  }
    
answered by 06.01.2017 / 19:58
source
1

You can try saving the data in integers and pass them to the field in this way:

    private Edittext texto;
    private int datePickerAnio;
    private int datePickerMes;
    private int datePickerDia;
    private int hora;
    private int minuto;



final Calendar cal = Calendar.getInstance();
            datePickerAnio = c.get(Calendar.YEAR);
            datePickerMes = c.get(Calendar.MONTH);
            datePickerDia = c.get(Calendar.DAY_OF_MONTH);
            hora = timePicker.getCurrentHour();
            minuto = timePicker.getCurrentMinute();

mostrarFecha();

private void mostrarFecha() {
        texto.setText(
            new StringBuilder()

                    .append(datePickerDia ).append("-")
                    .append(datePickerMes + 1).append("-")
                    .append(datePickerAnio ).append(" "));
    }
    
answered by 06.01.2017 в 20:13