[英]Android Material Design Inline Datepicker issue
I have a ScrollView
with a Datepicker我有一个带日期选择器的
ScrollView
In the previous versions of Android the Datepicker is this:在以前的 Android 版本中,Datepicker 是这样的:
And I can scroll the single elements of Datepicker day, month, years without problems我可以毫无问题地滚动 Datepicker 日、月、年的单个元素
In Android Lollipop API level 21 Material, the Datepiker is shown in this way:在 Android Lollipop API level 21 Material 中,Datepiker 是这样显示的:
If I click on the month is shown a calendar view and I cannot change the month, I can only select the day.如果我单击月份会显示日历视图并且我无法更改月份,我只能选择日期。 If I try to edit the year is shown a year scroller, but If I try tro scroll it, the whole layout where datepicker is contained is scrolled, not only the date.
如果我尝试编辑年份会显示一个年份滚动条,但是如果我尝试 tro 滚动它,则包含 datepicker 的整个布局都会滚动,而不仅仅是日期。
calendarViewShown=false
seems ignored似乎被忽略了
How could I fix this issue???我该如何解决这个问题???
The calendarViewShown
attribute is deprecated in the calendar-style date picker. calendarViewShown
样式的日期选择器中不推荐使用calendarViewShown
属性。 If you want the spinner-style date picker back, you can set the datePickerMode
attribute to spinner
.如果您想要
datePickerMode
器样式的日期选择器,您可以将datePickerMode
属性设置为spinner
。
<DatePicker
...
android:datePickerMode="spinner" />
As for the scrolling issue, the calendar-style date picker doesn't support nested scrolling.至于滚动问题,日历样式的日期选择器不支持嵌套滚动。
Step-1: Create spinner/calendar date picker layout步骤 1:创建微调器/日历日期选择器布局
.../main/res/layout/spinner_date_picker_layout.xml .../main/res/layout/spinner_date_picker_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:calendarViewShown="false" />
.../main/res/layout/calendar_date_picker_layout.xml .../main/res/layout/calendar_date_picker_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="calendar" />
Step-2: Set clickable behavior on TextView for showing Date Dialog.第 2 步:在 TextView 上设置可点击行为以显示日期对话框。
.../main/res/layout/activity_layout.xml .../main/res/layout/activity_layout.xml
<TextView
android:id="@+id/dateText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:clickable="true"
android:text="Date"
android:onClick="@{() -> viewModel.onClickDate()}"></TextView>
Step-3: Show Dialog on onClickDate第 3 步:在 onClickDate 上显示对话框
override fun onClickDate() {
showDialogForDate()
}
Step-4: Set DatePicker Layout into Dialog View.第 4 步:将 DatePicker 布局设置为对话框视图。
private fun showDialogForDate() {
//Set spinner/calendar date picker layout
val spinnerDatePicker = layoutInflater.inflate(R.layout.spinner_date_picker_layout, null)
// On click listener for dialog buttons
val dialogClickListener = DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
activity!!.dateText.text = spinnerDatePicker.datePicker.dayOfMonth.toString() + "/" + (spinnerDatePicker.datePicker.month + 1) + "/" + spinnerDatePicker.datePicker.year
}
DialogInterface.BUTTON_NEGATIVE -> {
}
}
}
val builder = AlertDialog.Builder(context!!)
builder.setTitle(resources.getString(R.string.dialog_title))
.setView(spinnerDatePicker)
.setPositiveButton("Ok", dialogClickListener)
.setNegativeButton("Cancel", dialogClickListener)
.create()
.show()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.