Widget DatePicker en la actividad para agregar receta

This commit is contained in:
AngelSuarez03 2024-05-22 08:31:35 -06:00
parent 2a21d6b537
commit 30a54fe6c6
3 changed files with 67 additions and 8 deletions

View File

@ -13,6 +13,8 @@ import android.widget.Toast
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.terratenientes.medicalhealth.databinding.ActivityRecetaBinding import com.terratenientes.medicalhealth.databinding.ActivityRecetaBinding
import com.terratenientes.medicalhealth.utilidad.DatePickerFragment
import java.util.Date
class RecetaActivity : AppCompatActivity(), OnItemSelectedListener { class RecetaActivity : AppCompatActivity(), OnItemSelectedListener {
private lateinit var binding : ActivityRecetaBinding private lateinit var binding : ActivityRecetaBinding
@ -33,6 +35,9 @@ class RecetaActivity : AppCompatActivity(), OnItemSelectedListener {
binding.btnAgregarReceta.setOnClickListener { binding.btnAgregarReceta.setOnClickListener {
agregarReceta() agregarReceta()
} }
binding.tvDatepicker.setOnClickListener{
showPickerDialog()
}
} }
@SuppressLint("Range") @SuppressLint("Range")
private fun obtenerNombrePacientes() : ArrayList<String>{ private fun obtenerNombrePacientes() : ArrayList<String>{
@ -57,12 +62,13 @@ class RecetaActivity : AppCompatActivity(), OnItemSelectedListener {
val edadPaciente = (binding.etEdadPaciente.text.toString()).toInt() val edadPaciente = (binding.etEdadPaciente.text.toString()).toInt()
val pesoPaciente = (binding.etPesoPaciente.text.toString()).toDouble() val pesoPaciente = (binding.etPesoPaciente.text.toString()).toDouble()
val medicamento= binding.etRecetaMedicamento.text.toString() 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 dosis = binding.etDosisMedicamento.text.toString()
val duracion= binding.etDuracion.text.toString() val duracion= binding.etDuracion.text.toString()
val receta = Receta(nombrePaciente,edadPaciente,pesoPaciente,medicamento,fecha,dosis,duracion) val receta = Receta(nombrePaciente,edadPaciente,pesoPaciente,medicamento,fecha,dosis,duracion)
db.agregarReceta(receta) db.agregarReceta(receta)
Toast.makeText(this@RecetaActivity , "RECETA AGREGADA CORRECTAMENTE", Toast.LENGTH_LONG).show() Toast.makeText(this@RecetaActivity , "RECETA AGREGADA CORRECTAMENTE", Toast.LENGTH_LONG).show()
finish()
} }
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) { override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
@ -74,4 +80,13 @@ class RecetaActivity : AppCompatActivity(), OnItemSelectedListener {
override fun onNothingSelected(p0: AdapterView<*>?) { 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"
}
} }

View File

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

View File

@ -51,15 +51,36 @@
android:hint="@string/receta_medicamento" android:hint="@string/receta_medicamento"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
/> />
<EditText <GridLayout
android:id="@+id/et_fecha"
android:layout_marginTop="10dp"
android:layout_width="300dp" android:layout_width="300dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="2"
android:rowCount="1">
<TextView
android:id="@+id/tv_fecha"
android:layout_marginTop="10dp"
android:layout_width="200dp"
android:layout_height="match_parent"
android:hint="@string/receta_fecha_paciente" android:hint="@string/receta_fecha_paciente"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:inputType="date" android:inputType="date"
/> android:layout_columnWeight="0"
android:layout_column="0"
android:layout_row="0"/>
<TextView
android:id="@+id/tv_datepicker"
android:layout_height="match_parent"
android:layout_width="100dp"
android:layout_marginTop="10dp"
android:textColor="@color/primaryColor"
android:text="calendario"
android:textSize="20sp"
android:layout_columnWeight="1"
android:layout_column="1"
android:layout_row="0"/>
</GridLayout>
<EditText <EditText
android:id="@+id/et_dosis_medicamento" android:id="@+id/et_dosis_medicamento"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"