android 时间选择器 (类似外卖跑腿下单)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a47c6dd976862d20831c5a5a48acb85c.png)
github链接
打开弹框
btnShow.setOnClickListener {
if (timeDialog == null) {
timeDialog = TimeDialogFragment(30)
} else {
timeDialog = TimeDialogFragment(30)
}
timeDialog!!.show(supportFragmentManager, "")
}
数据填充
private fun initData() {
dayList = StringHelper.getDay2()
wheelDay!!.adapter = MyAdapter(dayList!!)
val c = Calendar.getInstance()
hourList =
StringHelper.getHourTime(c.get(Calendar.HOUR_OF_DAY), timeNum, c.get(Calendar.MINUTE))
hourAdapter = MyAdapter(hourList!!)
wheelHour!!.adapter = hourAdapter
wheelDay!!.setOnItemSelectedListener { index ->
curDay = index
wheelHour!!.currentItem = 0
wheelMin!!.currentItem = 0
if (index == 0) {
hourAdapter = null
val currentHour = wheelHour!!.currentItem
if (currentHour == 0) {
wheelMin!!.visibility = View.GONE
} else {
wheelMin!!.visibility = View.VISIBLE
}
hourAdapter = MyAdapter(hourList!!)
wheelHour!!.adapter = hourAdapter
} else {
val currHour = c.get(Calendar.HOUR_OF_DAY)
val hour = timeNum / 60
if (currHour + hour > 23) {
hourAdapter = null
wheelMin!!.visibility = View.VISIBLE
twoHourList = StringHelper.getTwoHourTime(currHour + hour - 24)
hourAdapter = MyAdapter(twoHourList!!)
wheelHour!!.adapter = hourAdapter
minAdapter = null
minList = StringHelper.getMin(false, timeNum, c.get(Calendar.MINUTE))
minAdapter = MyAdapter(minList!!)
wheelMin!!.adapter = minAdapter
wheelMin!!.visibility = View.VISIBLE
} else {
hourAdapter = null
wheelMin!!.visibility = View.VISIBLE
twoHourList = StringHelper.getHourAll()
hourAdapter = MyAdapter(twoHourList!!)
wheelHour!!.adapter = hourAdapter
minAdapter = null
minList = StringHelper.getMin(true, 0, 0)
minAdapter = MyAdapter(minList!!)
wheelMin!!.adapter = minAdapter
wheelMin!!.visibility = View.VISIBLE
}
}
}
wheelHour!!.setOnItemSelectedListener { index ->
if (curDay == 0) {
if (index == 0) {
wheelMin!!.visibility = View.GONE
} else if (index == 1) {
minAdapter = null
minList = StringHelper.getMin(false, timeNum, c.get(Calendar.MINUTE))
minAdapter = MyAdapter(minList!!)
wheelMin!!.adapter = minAdapter
wheelMin!!.visibility = View.VISIBLE
} else {
minAdapter = null
minList = StringHelper.getMin(true, 0, 0)
minAdapter = MyAdapter(minList!!)
wheelMin!!.adapter = minAdapter
wheelMin!!.visibility = View.VISIBLE
}
} else {
val currHour = c.get(Calendar.HOUR_OF_DAY)
val hour = timeNum / 60
if (currHour + hour > 23) {
if (index == 0) {
minAdapter = null
minList = StringHelper.getMin(false, timeNum, c.get(Calendar.MINUTE))
minAdapter = MyAdapter(minList!!)
wheelMin!!.adapter = minAdapter
wheelMin!!.visibility = View.VISIBLE
} else {
minAdapter = null
minList = StringHelper.getMin(true, 0, 0)
minAdapter = MyAdapter(minList!!)
wheelMin!!.adapter = minAdapter
wheelMin!!.visibility = View.VISIBLE
}
} else {
minAdapter = null
minList = StringHelper.getMin(true, 0, 0)
minAdapter = MyAdapter(minList!!)
wheelMin!!.adapter = minAdapter
wheelMin!!.visibility = View.VISIBLE
}
}
}
}