Solución:
Se vería así:
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val dpd = DatePickerDialog(activity, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// Display Selected date in textbox
lblDate.setText("" + dayOfMonth + " " + MONTHS[monthOfYear] + ", " + year)
}, year, month, day)
dpd.show()
esto se hizo simplemente copiando y pegando el código en un archivo kotlin en Android Studio. Recomiendo encarecidamente su uso.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = findViewById(R.id.textView_date)
textView.text = SimpleDateFormat("dd.MM.yyyy").format(System.currentTimeMillis())
var cal = Calendar.getInstance()
val dateSetListener = DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
cal.set(Calendar.YEAR, year)
cal.set(Calendar.MONTH, monthOfYear)
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
val myFormat = "dd.MM.yyyy" // mention the format you need
val sdf = SimpleDateFormat(myFormat, Locale.US)
textView.text = sdf.format(cal.time)
}
textView.setOnClickListener {
DatePickerDialog([email protected], dateSetListener,
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)).show()
}
}
}
Ejemplo de Android de Kotlin Anko:
alert {
isCancelable = false
lateinit var datePicker: DatePicker
customView {
verticalLayout {
datePicker = datePicker {
maxDate = System.currentTimeMillis()
}
}
}
yesButton {
val parsedDate = "${datePicker.dayOfMonth}/${datePicker.month + 1}/${datePicker.year}"
}
noButton { }
}.show()
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)