如何在edittext中显示日期,并使用BindingAdapter和android Mvvm通过日期选择器进行更新
我有一个如何在edittext中显示日期,并使用BindingAdapter和android Mvvm通过日期选择器进行更新,android,kotlin,android-databinding,android-datepicker,android-mvvm,Android,Kotlin,Android Databinding,Android Datepicker,Android Mvvm,我有一个按钮和编辑文本我想将今天的日期设置为编辑文本,然后用户可以使用按钮设置新日期,并显示日期选择器以选择新日期。问题是我无法在edittext中设置当前日期,我可以显示datepicker和选择。我使用了BindingAdapter和MVVM我对这种方法不熟悉任何指导或帮助,请提前感谢 绑定适配器: @BindingAdapter("selectDate") fun bindDateClicks(button: Button,mutableLiveData: MutableLiveData&
按钮
和编辑文本
我想将今天的日期设置为编辑文本,然后用户可以使用按钮设置新日期,并显示日期选择器
以选择新日期。问题是我无法在edittext
中设置当前日期,我可以显示datepicker
和选择。我使用了BindingAdapter
和MVVM
我对这种方法不熟悉任何指导或帮助,请提前感谢
绑定适配器:
@BindingAdapter("selectDate")
fun bindDateClicks(button: Button,mutableLiveData: MutableLiveData<String>) {
button.setOnClickListener {
selectDate(button.context,mutableLiveData)
}
}
fun selectDate(context: Context,mutableLiveData: MutableLiveData<String>) {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog(context,
DatePickerDialog.OnDateSetListener {
view, year, monthOfYear, dayOfMonth ->
mutableLiveData.value = year.toString() + "-" + (monthOfYear + 1) + "-" + dayOfMonth.toString()
},year,month,day)
datePickerDialog.show()
}
<Button
android:text="set Date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:selectDate="@{viewModel.currentDate}"
app:layout_constraintTop_toBottomOf="@+id/spinner3"
android:layout_marginTop="48dp"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.005"/>
val currentDate = MutableLiveData<String>()
@BindingAdapter(“selectDate”)
趣味bindDateClicks(按钮:按钮,mutableLiveData:mutableLiveData){
button.setOnClickListener{
选择日期(button.context,mutableLiveData)
}
}
fun selectDate(上下文:context,mutableLiveData:mutableLiveData){
val c=Calendar.getInstance()
val year=c.get(日历年)
val month=c.get(Calendar.month)
val day=c.get(日历日/月)
val datePickerDialog=datePickerDialog(上下文,
DatePickerDialog.OnDateSetListener{
视图,年,月,月->
mutableLiveData.value=year.toString()+“-”+(monthOfYear+1)+“-”+dayOfMonth.toString()
}年、月、日)
datePickerDialog.show()
}
按钮:
@BindingAdapter("selectDate")
fun bindDateClicks(button: Button,mutableLiveData: MutableLiveData<String>) {
button.setOnClickListener {
selectDate(button.context,mutableLiveData)
}
}
fun selectDate(context: Context,mutableLiveData: MutableLiveData<String>) {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog(context,
DatePickerDialog.OnDateSetListener {
view, year, monthOfYear, dayOfMonth ->
mutableLiveData.value = year.toString() + "-" + (monthOfYear + 1) + "-" + dayOfMonth.toString()
},year,month,day)
datePickerDialog.show()
}
<Button
android:text="set Date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:selectDate="@{viewModel.currentDate}"
app:layout_constraintTop_toBottomOf="@+id/spinner3"
android:layout_marginTop="48dp"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.005"/>
val currentDate = MutableLiveData<String>()
视图模型:
@BindingAdapter("selectDate")
fun bindDateClicks(button: Button,mutableLiveData: MutableLiveData<String>) {
button.setOnClickListener {
selectDate(button.context,mutableLiveData)
}
}
fun selectDate(context: Context,mutableLiveData: MutableLiveData<String>) {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog(context,
DatePickerDialog.OnDateSetListener {
view, year, monthOfYear, dayOfMonth ->
mutableLiveData.value = year.toString() + "-" + (monthOfYear + 1) + "-" + dayOfMonth.toString()
},year,month,day)
datePickerDialog.show()
}
<Button
android:text="set Date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:selectDate="@{viewModel.currentDate}"
app:layout_constraintTop_toBottomOf="@+id/spinner3"
android:layout_marginTop="48dp"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.005"/>
val currentDate = MutableLiveData<String>()
val currentDate=MutableLiveData()
首先,
对话框应该是活动/片段的一部分
考虑到这一点,您实际上不需要有适配器。
考虑数据绑定—您可能希望使用单独的片段在其中包含日期选择器。在这种情况下,您可以使用共享ViewModel来共享日期值
请注意,在自定义绑定适配器中,您可以使用
String
而不是LiveData
在ViewModel中创建日期的实时数据或可观察字段(最初指定默认日期)
日期选择器对话框应为活动或片段级别。
当日期更改表单日期选择器时,将日期更新到ViewModel中。将Viewmodel定义到布局文件中。我试图将其放在片段中,但遇到了上下文问题什么问题?您能提供更多详细信息吗?我使用Androidviewmodel来使用上下文,但当我将上下文放入对话框时无法解析此链接(西班牙语)可能有助于“日期选择器对话框应为活动或片段级别…”感谢您在博客中给出的解决方案。即使不懂西班牙语,它也是关于如何在Kotlin和Java中建立片段和日期选择器之间关系的非常有用的信息源