web-dev-qa-db-fra.com

Comment utiliser DatePickerDialog dans Kotlin?

Dans Java un DatePickerDialog peut être utilisé tel que:

final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);


DatePickerDialog dpd = new DatePickerDialog(getActivity(),new DatePickerDialog.OnDateSetListener()
{
   @Override
   public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
  {
      // Display Selected date in textbox
      lblDate.setText(""+ dayOfMonth+" "+MONTHS[monthOfYear] + ", " + year);
  }
}, year, month,day);
dpd.show();

À quoi ressemble l'utilisation de DatePickerDialog de Kotlin?

17
Adil Saiyad

Cela ressemblerait à quelque chose comme ceci:

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()

cela a été fait en copiant et collant simplement le code dans un fichier kotlin dans Android studio. Je recommanderais fortement de l'utiliser.

42
Derek
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(this@MainActivity, dateSetListener,
                    cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH),
                    cal.get(Calendar.DAY_OF_MONTH)).show()
        }
    }
}
9

Kotlin Anko Android Exemple:

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()
8
swapnil

utiliser le sélecteur de date sur le clic du bouton

   val cal = Calendar.getInstance()
    val y = cal.get(Calendar.YEAR)
    val m = cal.get(Calendar.MONTH)
    val d = cal.get(Calendar.DAY_OF_MONTH)


    val datepickerdialog:DatePickerDialog = DatePickerDialog(activity, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->

    // Display Selected date in textbox
    lblDate.setText("" + dayOfMonth + " " + MONTHS[monthOfYear] + ", " + year)
        }, y, m, d)

    datepickerdialog.show()

et cela utilisé pour afficher le sélecteur de temps sur le clic du bouton

textView54.setOnClickListener {
                val c:Calendar= Calendar.getInstance()
                val hh=c.get(Calendar.HOUR_OF_DAY)
                val mm=c.get(Calendar.MINUTE)
                val timePickerDialog:TimePickerDialog=TimePickerDialog(this@VendorRegistration,TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
                    textView54.setText( ""+hourOfDay + ":" + minute);
                },hh,mm,true)
                timePickerDialog.show()
0
Prabh deep