日历未保存 time/date 从 time/date 选择器中选择

Calendar not saving time/date picked from time/date picker

android 的新手,所以我不确定我是否使用了正确的术语。我有一个日期和时间选择器,我试图将其条目保存到单个日历实例中,但是选择的时间和日期没有保存到日历中,而是当前时间和日期。我不确定我做错了什么。

class AddReminderActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_reminder)
       
        val timePicker: TimePicker = findViewById(R.id.reminder_time)
        val datePicker: DatePicker = findViewById(R.id.reminder_date)

        val combinedCal = GregorianCalendar(TimeZone.getTimeZone("PDT"))
        combinedCal.set(datePicker.year, datePicker.month, datePicker.dayOfMonth, timePicker.hour, timePicker.minute)

        val button: Button = findViewById(R.id.reminder_save_button)
        button.setOnClickListener{
            Toast.makeText(it.context, combinedCal.time.toString(), Toast.LENGTH_LONG).show()
        }

    }
}

而不是这个:

val combinedCal = GregorianCalendar(TimeZone.getTimeZone("PDT"))
combinedCal.set(datePicker.year, datePicker.month, datePicker.dayOfMonth, timePicker.hour, timePicker.minute)

val button: Button = findViewById(R.id.reminder_save_button)
button.setOnClickListener{
    Toast.makeText(it.context, combinedCal.time.toString(), Toast.LENGTH_LONG).show()
}

尝试这样做:

val button: Button = findViewById(R.id.reminder_save_button)
button.setOnClickListener{
    val combinedCal = GregorianCalendar(TimeZone.getTimeZone("PDT"))
    combinedCal.set(datePicker.year, datePicker.month, datePicker.dayOfMonth, timePicker.hour, timePicker.minute)
    Toast.makeText(it.context, combinedCal.time.toString(), Toast.LENGTH_LONG).show()
}

使用 DatePicker.setOnDateChangedListener() 收听变化并据此更改您的日历值。

从 26 API 开始,我们可以像这样设置监听器 DatePicker.setOnDateChangedListener()。但是对于之前的版本,我们需要首先用我们想要显示的日期来初始化它。这里我们也可以添加监听器。

所以我们首先检查构建版本并为 android 的不同版本编写两个不同的代码。

您的代码应如下所示-

class AddReminderActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_add_reminder)
   
    val timePicker: TimePicker = findViewById(R.id.reminder_time)
    val datePicker: DatePicker = findViewById(R.id.reminder_date)

    val combinedCal = GregorianCalendar(TimeZone.getTimeZone("PDT"))
    combinedCal.set(datePicker.year, datePicker.month, datePicker.dayOfMonth, timePicker.hour, timePicker.minute)

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                dp_picker.setOnDateChangedListener { _, year, monthOfYear, dayOfMonth ->
                    combinedCal.set(
                        year,
                        monthOfYear,
                        dayOfMonth
                    )
                }

                time_picker.setOnTimeChangedListener { _, hourOfDay, minute ->
                    combinedCal.time.hours = hourOfDay
                    combinedCal.time.minutes = minute
                }
            } else {
                dp_picker.init(
                    combinedCal.get(Calendar.YEAR),
                    combinedCal.get(Calendar.MONTH),
                    combinedCal.get(Calendar.DAY_OF_MONTH)
                ) { _, year, monthOfYear, dayOfMonth ->
                    combinedCal.set(
                        year,
                        monthOfYear,
                        dayOfMonth
                    )
                }

                time_picker.setOnTimeChangedListener { _, hourOfDay, minute ->
                    combinedCal.time.hours = hourOfDay
                    combinedCal.time.minutes = minute
                }
            }

    val button: Button = findViewById(R.id.reminder_save_button)
    button.setOnClickListener{
        Toast.makeText(it.context, combinedCal.time.toString(), Toast.LENGTH_LONG).show()
    }

}