我有一段代码,当在活动的主类中按下按钮时,就会启动DatePickerFragment
和TimePickerFragment
。
fun showDatePickerDialog(view: View) {
val newFragment = DatePickerFragment()
newFragment.show(supportFragmentManager, "datePicker")
}
fun showTimePickerDialog(view: View) {
TimePickerFragment().show(supportFragmentManager, "timePicker")
}
接下来,我使用了文档中的this guide来启动此过程-涉及类class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener
和class TimePickerFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener
。这是我出现问题的地方。这就是我想要做的:
class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// Use the current date as the default date in the picker
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
// Create a new instance of DatePickerDialog and return it
return DatePickerDialog(activity, this, year, month, day)
}
override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
// Do something with the date chosen by the user
var dateText = findViewbyId<textview>(R.id.text_newRem_date)
dateText.text("$day/$month/$year")
}
}
简而言之,我想编辑text_newRem_date
TextView中的文本以反映为用户选择的日期,时间同样适用,但过程将完全相同。我不熟悉类的工作方式,所以有更好的方法吗?实际上,我希望在每个类之间传递数据。
我是Kotlin开发人员的新手,如果这真的很基础,请原谅我!