如何使用日期选择器之外的变量(How to use variables outside the da

2019-10-30 03:47发布

一天,一年和一个月更新后使用这些给定的值。 我希望他们使用更新值,请帮助。

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 

Answer 1:

创建一个临时日历,并设置所选的日期到日历。 之后,从外从日历中获取数据。

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)


Answer 2:

简单地做一个新的方法,并通过您的参数

    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 

}


文章来源: How to use variables outside the date picker