我在DatePickerDialog.OnDateSetListener的下面的函数中声明和初始化的变量很少:
fun pickDate(view: View) {
val eventDate = (findViewById<TextView>(R.id.event_date_text_))
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 months = arrayOf("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
var monthOfYear = 0
val dpd = DatePickerDialog(
this,
DatePickerDialog.OnDateSetListener { _, year, monthOfYear, dayOfMonth ->
val date = "" + dayOfMonth + " " + months[monthOfYear] + " " + year
eventDate.text = date
},
year,
month,
day
)
dpd.show()
}
我还有另一个函数createEvent(view:View),如何在同一类的createEvent函数中访问dayOfMonth,monthOfYear和Year?