答案 0 :(得分:5)
答案 1 :(得分:5)
对于Android的默认材料组件,您可以使用新的MaterialDatePicker。您可以在其中获得一个日期选择以及日期范围选择。
将以下内容添加到您的build.gradle
implementation 'com.google.android.material:material:1.2.0 alpha02'
调用以下方法打开日期范围选择器
private fun setupRangePickerDialog() {
val builder: MaterialDatePicker.Builder<*> = MaterialDatePicker.Builder.dateRangePicker()
val constraintsBuilder = CalendarConstraints.Builder()
try {
builder.setCalendarConstraints(constraintsBuilder.build())
val picker: MaterialDatePicker<*> = builder.build()
getDateRange(picker)
picker.show(supportFragmentManager, picker.toString())
} catch (e: IllegalArgumentException) {
}
}
现在在下面放置获取开始和结束日期的方法
private fun getDateRange(materialCalendarPicker: MaterialDatePicker<out Any>) {
materialCalendarPicker.addOnPositiveButtonClickListener(
{ selection: Any? ->
Log.e("DateRangeText",materialCalendarPicker.headerText)
})
materialCalendarPicker.addOnNegativeButtonClickListener(
{ dialog: View? ->
}
)
materialCalendarPicker.addOnCancelListener(
{ dialog: DialogInterface? ->
}
)
}
恭喜,您完成了!
答案 2 :(得分:1)
implementation 'com.google.android.material:material:1.3.0-alpha01'
使用此依赖项将其添加到build.gradle
这是在按钮上单击设置此代码 :
MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
builder.setTheme(R.style.CustomThemeOverlay_MaterialCalendar_Fullscreen);
builder.setCalendarConstraints(constraintsBuilder.build());
MaterialDatePicker picker = builder.build();
assert getFragmentManager() != null;
picker.show(getFragmentManager(), picker.toString());
添加到您的style.xml中:
<style name="CustomThemeOverlay_MaterialCalendar_Fullscreen" parent="@style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen">
<item name="materialCalendarStyle">@style/Custom_MaterialCalendar.Fullscreen</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorOnSurface">@color/colorTextPrimary</item>
</style>
<style name="Custom_MaterialCalendar.Fullscreen" parent="@style/Widget.MaterialComponents.MaterialCalendar.Fullscreen">
<item name="android:windowFullscreen">false</item>
</style>
在您的整个项目样式Apptheme中添加:
<item name="materialCalendarStyle">@style/Widget.MaterialComponents.MaterialCalendar</item>
<item name="materialCalendarFullscreenTheme">@style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen</item>
<item name="materialCalendarTheme">@style/ThemeOverlay.MaterialComponents.MaterialCalendar</item>
答案 3 :(得分:0)
使用此自定义日历, 在build.gradle
中SELECT
1001,
CAST(AYear AS VARCHAR(4)) + '-' + AQuarter,
NULL,NULL
FROM
(VALUES(2021), (2022), (2023), (2024), (2025), (2026), (2027), (2028), (2029), (2030)) AS YT(AYear)
CROSS JOIN
(VALUES('Q1'), ('Q2'), ('Q3'), ('Q4')) AS QT(AQuarter)
ORDER BY
AYear, AQUarter
和xml文件
implementation 'com.savvi.datepicker:rangepicker:1.2.0'
和Java文件
<com.savvi.rangedatepicker.CalendarPickerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/calendar_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:scrollbarStyle="outsideOverlay"
android:clipToPadding="false"
app:tsquare_dividerColor="@color/calendar_bg"
app:tsquare_headerTextColor="@color/calendar_selected_day_bg"
/>