From 30a54fe6c66f24eed7ea95bb457567fd12a9b6fd Mon Sep 17 00:00:00 2001 From: AngelSuarez03 Date: Wed, 22 May 2024 08:31:35 -0600 Subject: [PATCH] Widget DatePicker en la actividad para agregar receta --- .../medicalhealth/RecetaActivity.kt | 17 ++++++++- .../utilidad/DatePickerFragment.kt | 23 ++++++++++++ app/src/main/res/layout/activity_receta.xml | 35 +++++++++++++++---- 3 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/terratenientes/medicalhealth/utilidad/DatePickerFragment.kt diff --git a/app/src/main/java/com/terratenientes/medicalhealth/RecetaActivity.kt b/app/src/main/java/com/terratenientes/medicalhealth/RecetaActivity.kt index aad1018..6f456f8 100644 --- a/app/src/main/java/com/terratenientes/medicalhealth/RecetaActivity.kt +++ b/app/src/main/java/com/terratenientes/medicalhealth/RecetaActivity.kt @@ -13,6 +13,8 @@ import android.widget.Toast import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import com.terratenientes.medicalhealth.databinding.ActivityRecetaBinding +import com.terratenientes.medicalhealth.utilidad.DatePickerFragment +import java.util.Date class RecetaActivity : AppCompatActivity(), OnItemSelectedListener { private lateinit var binding : ActivityRecetaBinding @@ -33,6 +35,9 @@ class RecetaActivity : AppCompatActivity(), OnItemSelectedListener { binding.btnAgregarReceta.setOnClickListener { agregarReceta() } + binding.tvDatepicker.setOnClickListener{ + showPickerDialog() + } } @SuppressLint("Range") private fun obtenerNombrePacientes() : ArrayList{ @@ -57,12 +62,13 @@ class RecetaActivity : AppCompatActivity(), OnItemSelectedListener { val edadPaciente = (binding.etEdadPaciente.text.toString()).toInt() val pesoPaciente = (binding.etPesoPaciente.text.toString()).toDouble() val medicamento= binding.etRecetaMedicamento.text.toString() - val fecha=binding.etFecha.text.toString() + val fecha=binding.tvFecha.text.toString() val dosis = binding.etDosisMedicamento.text.toString() val duracion= binding.etDuracion.text.toString() val receta = Receta(nombrePaciente,edadPaciente,pesoPaciente,medicamento,fecha,dosis,duracion) db.agregarReceta(receta) Toast.makeText(this@RecetaActivity , "RECETA AGREGADA CORRECTAMENTE", Toast.LENGTH_LONG).show() + finish() } override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) { @@ -74,4 +80,13 @@ class RecetaActivity : AppCompatActivity(), OnItemSelectedListener { override fun onNothingSelected(p0: AdapterView<*>?) { } + + private fun showPickerDialog() { + val datePicker = DatePickerFragment{day, month, year -> obtenerFechaSeleccionada(day, month, year)} + datePicker.show(supportFragmentManager,"datePicker") + } + + private fun obtenerFechaSeleccionada (day: Int, month: Int, year: Int) { + binding.tvFecha.text = "$day/$month/$year" + } } \ No newline at end of file diff --git a/app/src/main/java/com/terratenientes/medicalhealth/utilidad/DatePickerFragment.kt b/app/src/main/java/com/terratenientes/medicalhealth/utilidad/DatePickerFragment.kt new file mode 100644 index 0000000..95451e3 --- /dev/null +++ b/app/src/main/java/com/terratenientes/medicalhealth/utilidad/DatePickerFragment.kt @@ -0,0 +1,23 @@ +package com.terratenientes.medicalhealth.utilidad + +import android.app.DatePickerDialog +import android.app.Dialog +import android.content.Context +import android.os.Bundle +import android.widget.DatePicker +import androidx.fragment.app.DialogFragment +import java.util.Calendar + +class DatePickerFragment (val listener: (day: Int, month: Int, year: Int) -> Unit): DialogFragment(), DatePickerDialog.OnDateSetListener { + override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) { + listener(dayOfMonth, month, year) + } + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val c: Calendar = Calendar.getInstance() + val day: Int = c.get(Calendar.DAY_OF_MONTH) + val month: Int = c.get(Calendar.MONTH) + val year: Int = c.get(Calendar.YEAR) + return DatePickerDialog(activity as Context, this@DatePickerFragment, year, month, day) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_receta.xml b/app/src/main/res/layout/activity_receta.xml index c4d0cc1..22d0eaa 100644 --- a/app/src/main/res/layout/activity_receta.xml +++ b/app/src/main/res/layout/activity_receta.xml @@ -51,15 +51,36 @@ android:hint="@string/receta_medicamento" android:layout_gravity="center_horizontal" /> - + android:layout_gravity="center" + android:columnCount="2" + android:rowCount="1"> + + +