在视图中输入星期几的简单方法是什么?
whats a simple way of inputting days of the week in a view?
所以我有 3 个文本视图来显示未来 3 天的预报。我使用视图绑定来绑定视图,但我觉得有一种更简单的编写方法,但我想不出解决方案。我如何遍历一周中的几天并相应地设置文本视图?
'''
// getting day of the week
private val dayOfWeek: DayOfWeek = LocalDate.now().dayOfWeek
// getting day of week as int value
private val dayNum: Int = dayOfWeek.value
'''
''' // 1 = monday etc
private fun forecastDays() {
when (dayNum){
// forecast of next three days
1 -> {
binding.tvDay1.text = "Tuesday"
binding.tvDay2.text = "Wednesday"
binding.tvDay3.text = "Thursday"
}
2 -> {
binding.tvDay1.text = "Wednesday"
binding.tvDay2.text = "Thursday"
binding.tvDay3.text = "Friday"
}
3 -> {
binding.tvDay1.text = "Thursday"
binding.tvDay2.text = "Friday"
binding.tvDay3.text = "Saturday"
}
4 -> {
binding.tvDay1.text = "Friday"
binding.tvDay2.text = "Saturday"
binding.tvDay3.text = "Sunday"
}
.. etc
'''
我猜你可以用 DateFormatter
来完成。这是另一种使用地图的方法。你可以将 daysMap
作为常数。
val daysMap = mapOf(
1 to "Sunday", 2 to "Monday", 3 to "Tuesday", 4 to "Wednesday", 5 to "Thursday", 6 to "Friday", 7 to "Saturday"
)
val dayOfWeek: DayOfWeek = LocalDate.now().dayOfWeek
val dayNum: Int = dayOfWeek.value
binding.tvDay1.text = daysMap[(dayNum + 1) % 7]
binding.tvDay2.text = daysMap[(dayNum + 2) % 7]
binding.tvDay3.text = daysMap[(dayNum + 3) % 7]
DayOfWeek is an Enum,意思就是可以用序号作为索引来获取DayOfWeek的数值。
// getting day of the week
private val dayOfWeek: DayOfWeek = LocalDate.now().dayOfWeek
// ordinal
private val dayNum: Int = dayOfWeek.ordinal()
...
private fun forecastDays() {
binding.tvDay1.text = DayOfWeek.values()[(dayNum + 1) % 7].name()
binding.tvDay2.text = DayOfWeek.values()[(dayNum + 2) % 7].name()
binding.tvDay3.text = DayOfWeek.values()[(dayNum + 3) % 7].name()
...
所以我有 3 个文本视图来显示未来 3 天的预报。我使用视图绑定来绑定视图,但我觉得有一种更简单的编写方法,但我想不出解决方案。我如何遍历一周中的几天并相应地设置文本视图?
'''
// getting day of the week
private val dayOfWeek: DayOfWeek = LocalDate.now().dayOfWeek
// getting day of week as int value
private val dayNum: Int = dayOfWeek.value
'''
''' // 1 = monday etc
private fun forecastDays() {
when (dayNum){
// forecast of next three days
1 -> {
binding.tvDay1.text = "Tuesday"
binding.tvDay2.text = "Wednesday"
binding.tvDay3.text = "Thursday"
}
2 -> {
binding.tvDay1.text = "Wednesday"
binding.tvDay2.text = "Thursday"
binding.tvDay3.text = "Friday"
}
3 -> {
binding.tvDay1.text = "Thursday"
binding.tvDay2.text = "Friday"
binding.tvDay3.text = "Saturday"
}
4 -> {
binding.tvDay1.text = "Friday"
binding.tvDay2.text = "Saturday"
binding.tvDay3.text = "Sunday"
}
.. etc
'''
我猜你可以用 DateFormatter
来完成。这是另一种使用地图的方法。你可以将 daysMap
作为常数。
val daysMap = mapOf(
1 to "Sunday", 2 to "Monday", 3 to "Tuesday", 4 to "Wednesday", 5 to "Thursday", 6 to "Friday", 7 to "Saturday"
)
val dayOfWeek: DayOfWeek = LocalDate.now().dayOfWeek
val dayNum: Int = dayOfWeek.value
binding.tvDay1.text = daysMap[(dayNum + 1) % 7]
binding.tvDay2.text = daysMap[(dayNum + 2) % 7]
binding.tvDay3.text = daysMap[(dayNum + 3) % 7]
DayOfWeek is an Enum,意思就是可以用序号作为索引来获取DayOfWeek的数值。
// getting day of the week
private val dayOfWeek: DayOfWeek = LocalDate.now().dayOfWeek
// ordinal
private val dayNum: Int = dayOfWeek.ordinal()
...
private fun forecastDays() {
binding.tvDay1.text = DayOfWeek.values()[(dayNum + 1) % 7].name()
binding.tvDay2.text = DayOfWeek.values()[(dayNum + 2) % 7].name()
binding.tvDay3.text = DayOfWeek.values()[(dayNum + 3) % 7].name()
...