如何在Datepicker Andriod Studio Kotlin中更新日期月份和年份

时间:2018-12-03 15:50:10

标签: kotlin datepicker

  

在更新后使用这些给定值的日,年和月。       我希望他们使用更新的值,请帮忙。

var day = 1  
var month = 1  
var year = 1999  
val dpd = DatePickerDialog(this,android.R.style.Theme_Holo_Dialog,
    DatePickerDialog.OnDateSetListener { datePicker, selyear,
   monthOfYear, dayOfMonth ->

    day = dayOfMonth
    month = monthOfYear
    year = selyear
    tv.text = "$day - $month - $year"  /*printing day month year*/

   }, year, month, day
 )

  dpd.show()

   val pday = 30 - day   /* day = 1(Not the chosen value)*/
   val pmonth = 12 - month  
   val pyear = 2019 - year 

1 个答案:

答案 0 :(得分:0)

将变量声明从val更改为var并在侦听器中更新它们:

        val c = Calendar.getInstance()
        var day = c.get(Calendar.DAY_OF_MONTH)
        var month = c.get(Calendar.MONTH)
        var year = c.get(Calendar.YEAR)

        val dpd = DatePickerDialog(
            this,
            android.R.style.Theme_Holo_Dialog,
            DatePickerDialog.OnDateSetListener { datePicker, selyear, monthOfYear, dayOfMonth ->
                day = dayOfMonth
                month = monthOfYear + 1
                year = selyear
                tv.text = "$day - $month - $year"

            }, year, month, day
        )

        dpd.show()