如何在日期选择器之外使用变量

时间:2019-01-03 12:02:46

标签: android kotlin

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

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 

2 个答案:

答案 0 :(得分:0)

制作一个临时日历并为该日历设置一个选定的日期。从外面从那个日历中获取数据之后。

val temp_calender = Calendar.getInstance()

    val dpd = DatePickerDialog(this,android.R.style.Theme_Holo_Dialog,
        DatePickerDialog.OnDateSetListener { datePicker, selyear,
       monthOfYear, dayOfMonth ->

        temp_calender.set(Calendar.YEAR, selyear)
                cal.set(Calendar.MONTH, monthOfYear)
                cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)

       }, year, month, day
     )

      dpd.show()


    // Get Out Side

        val year = temp_calender.get(Calendar.YEAR)
        val month = temp_calender.get(Calendar.MONTH)
        val day = temp_calender.get(Calendar.DAY_OF_MONTH)

答案 1 :(得分:0)

只需创建一个新方法并传递您的参数

    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 ->
       updateDate(dayOfMonth,monthOfYear,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 

updatDate(var d,var m ,var s){
    day = d
    month = m  
    year = s 

}