0

我正在尝试从 DatePicker 获取值,但我不知道如何管理它。

我的布局包含一个 DatePicker 元素,我正在使用 kotlin。

TimePicker 也有同样的问题。

4

2 回答 2

2

感谢马顿·苏斯

这个帮助类对 DatePicker 和 TimePicker 都有魔力:

data class DatePickerChangeEvent(
        val view: View,
        val year: Int = 0,
        val monthOfYear: Int = 0,
        val dayOfMonth: Int = 0
)

data class TimePickerChangeEvent(
        val view: View,
        val hour: Int,
        val minute: Int
)

 class DatePickerObservable(val view: DatePicker, val year: Int, val monthOfYear: Int, val dayOfMonth: Int)
    : io.reactivex.Observable<DatePickerChangeEvent>() {

    override fun subscribeActual(observer: Observer<in DatePickerChangeEvent>) {
        val listener = DateChangeListener(view, observer)
        observer.onSubscribe(listener)
        view.init(year, monthOfYear, dayOfMonth, listener)
    }

    class DateChangeListener(private val dp: DatePicker, private val obs: io.reactivex.Observer<in DatePickerChangeEvent>)
        : DatePicker.OnDateChangedListener, MainThreadDisposable() {

        override fun onDateChanged(p0: DatePicker, p1: Int, p2: Int, p3: Int) {
            obs.onNext(DatePickerChangeEvent(dp, p1, p2, p3))
        }

        override fun onDispose() {
        }
    }
}

class TimePickerObservable(val view: TimePicker)
    : io.reactivex.Observable<TimePickerChangeEvent>() {

    override fun subscribeActual(observer: Observer<in TimePickerChangeEvent>) {
        val listener = TimeChangeListener(view, observer)
        observer.onSubscribe(listener)
        view.setOnTimeChangedListener(listener)
    }

    class TimeChangeListener(private val tp: TimePicker, private val obs: io.reactivex.Observer<in TimePickerChangeEvent>)
        : TimePicker.OnTimeChangedListener, MainThreadDisposable() {

        override fun onTimeChanged(p0: TimePicker, p1: Int, p2: Int) {
            obs.onNext(TimePickerChangeEvent(p0, p1, p2))
        }

        override fun onDispose() {
            tp.setOnTimeChangedListener { timePicker, i, i2 -> }
        }
    }
}

@Suppress("NOTHING_TO_INLINE")
inline fun DatePicker.dateChange(year: Int, monthOfYear: Int, dayOfMonth: Int): DatePickerObservable =
        DatePickerObservable(this, year, monthOfYear, dayOfMonth)

@Suppress("NOTHING_TO_INLINE")
inline fun TimePicker.timeChange(): TimePickerObservable =
        TimePickerObservable(this)

你可以这么称呼它:

 datePicker.dateChange(2018,9,15)
                .subscribe{
                    Log.d("RxDate", it.toString())
                }
于 2018-08-15T16:58:57.347 回答
0

你可以说得更详细点吗?您想向用户显示 DatePicker 和 TimePicker 并获取用户选择的值吗?在这种情况下,您可以像这样尝试。对于日期选择器,

val mCalendar = Calendar.getInstance()
val mYear = mCalendar.get(Calendar.YEAR)
val mMonth = mCalendar.get(Calendar.MONTH)
val mDay = mCalendar.get(Calendar.DAY_OF_MONTH)

val dpd = DatePickerDialog(
      this, DatePickerDialog.OnDateSetListener { _, thisYear, thisMonth, thisDay ->

    mYear = thisYear
    mMonth = thisMonth
    mDay = thisDay

    tDate.text = mDay.toString() + "/" + (mMonth + 1).toString() +
        "/" + mYear.toString()

  }, mYear, mMonth, mDay
  )
  dpd.show()

对于时间选择器,

TimePickerDialog(this, TimePickerDialog.OnTimeSetListener { _, hourOfDay, minute ->
tvHour.text = Calendar.HOUR_OF_DAY, hourOfDay
tvMinute.text = Calendar.MINUTE, minute
}, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), false).show()
于 2018-08-15T16:27:41.767 回答