我的应用程序中有一个水平日历。所以我实现了以下库 https://github.com/Mulham-Raee/Horizontal-Calendar 我还实现了自己的后退和前进按钮。
从当前日期按后退一天,向前按一天。假设今天是 8 月 10 日,我水平滚动到 7 月 15 日,没有按下后退或前进按钮。现在,如果我按下后退按钮而不是转到 7 月 14 日,我将返回到 8 月 9 日。
以下是我的代码
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.myapplication.HorizontalCalendarView
android:id="@+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:selectedDateBackground="@drawable/selected_date"
/>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:text="Back"
app:layout_constraintBottom_toTopOf="@+id/calendarView"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:text="Forward"
app:layout_constraintBottom_toTopOf="@+id/calendarView"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
Kotlin 代码:
class MainActivity : AppCompatActivity() {
private var counter: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_2)
val format1 = SimpleDateFormat("yyyy-MM-dd", Locale.US)
val currentDate = format1.format(Calendar.getInstance().time)
Toast.makeText(this,currentDate.toString(),Toast.LENGTH_LONG).show()
val startDate = Calendar.getInstance()
startDate.add(Calendar.MONTH, -10)
val endDate = Calendar.getInstance()
endDate.add(Calendar.MONTH, 30)
val horizontalCalendar = HorizontalCalendar.Builder(this, R.id.calendarView)
.range(startDate, endDate)
.datesNumberOnScreen(7)
.build()
horizontalCalendar.setCalendarListener(object : HorizontalCalendarListener() {
override fun onDateSelected(date: Calendar, position: Int) {
val selectedDate = format1.format(date.time)
Toast.makeText(this@MainActivity,
"Pritish"+SimpleDateFormat("MMMM yyyy", Locale.US).format(date.time),
Toast.LENGTH_SHORT).show()
if(format1.parse(selectedDate).before(format1.parse(currentDate))){
Toast.makeText(this@MainActivity,"Cannot selecte previous date",Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this@MainActivity,
selectedDate
,Toast.LENGTH_LONG).show()
}
}
override fun onCalendarScroll(calendarView: HorizontalCalendarView,
dx: Int, dy: Int) {
Log.i("Pritish",dx.toString()+dy.toString())
counter += if(dx<0){
-1
} else {
1
}
}
override fun onDateLongClicked(date: Calendar, position: Int): Boolean {
return true
}
})
val cal = Calendar.getInstance()
back.setOnClickListener {
cal.add(Calendar.DAY_OF_MONTH,counter-1)
counter = 0
horizontalCalendar.selectDate(cal,false)
horizontalCalendar.refresh()
}
forward.setOnClickListener {
cal.add(Calendar.DAY_OF_MONTH,counter+1)
counter = 0
horizontalCalendar.selectDate(cal,false)
horizontalCalendar.refresh()
}
}
}
最佳答案
看起来 onDateSelected
可能会在滚动期间被触发,您可以使用它加上一个简单的 +1/-1,但您必须尝试一下看看它是否有效-- 很难仅基于查看库的代码来验证。
关于android - 从日历中减去天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51788255/