以下代码段“有效”在某种意义上正确地更新了statusDateID。我遇到的问题是,尽管它正确设置了statusDateID,但日期不会持续存在,而当我更新mh.entryDate时,它始终是当前日期。
如何在对话框外获取日期。这样我就可以使用所选日期而非当前日期更新mh.entryDate。
感谢您的时间。
fun changeDate(view: View) {
var date: Calendar = Calendar.getInstance()
var thisAYear = date.get(Calendar.YEAR).toInt()
var thisAMonth = date.get(Calendar.MONTH).toInt()
var thisADay = date.get(Calendar.DAY_OF_MONTH).toInt()
val dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view2, thisYear, thisMonth, thisDay ->
// Display Selected date in textbox
thisAMonth = thisMonth + 1
thisADay = thisDay
thisAYear = thisYear
statusDateID.setText("Date: " + thisAMonth + "/" + thisDay + "/" + thisYear)
val newDate:Calendar =Calendar.getInstance()
newDate.set(thisYear, thisMonth, thisDay)
}, thisAYear, thisAMonth, thisADay)
dpd.show()
mh.entryDate = date.timeInMillis
println("DATE DATA: "+thisAYear+ " "+thisAMonth+" " + thisADay)
println("DATE CHANGED MILLISECS = "+mh.entryDate)
}
mh.entryDate定义为全局和LONG。使用调试的println语句,DATE DATA和DATE CHANGED MILLISECS都显示当前日期。
只需在DatePickerDialog.OnDateSetListener
方法中设置日期mh.entryDate = newDate.timeInMillis
val dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view2, thisYear, thisMonth, thisDay ->
// Display Selected date in textbox
thisAMonth = thisMonth + 1
thisADay = thisDay
thisAYear = thisYear
statusDateID.setText("Date: " + thisAMonth + "/" + thisDay + "/" + thisYear)
val newDate:Calendar =Calendar.getInstance()
newDate.set(thisYear, thisMonth, thisDay)
mh.entryDate = newDate.timeInMillis // setting new date
}, thisAYear, thisAMonth, thisADay)
dpd.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句