[英]how to set date picker in fragment using Kotlin?
我想使用这个库https://github.com/wdullaer/MaterialDateTimePicker显示日期选择器对话框
implementation "com.wdullaer:materialdatetimepicker:3.6.4"
这是我的片段中的代码
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog
val now = Calendar.getInstance()
val currentYear: Int = now.get(Calendar.YEAR)
val currentMonth: Int = now.get(Calendar.MONTH)
val currentDay: Int = now.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog.newInstance(DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// do something here
}, currentYear, currentMonth, currentDay)
datePickerDialog.setTitle("INI JUDUL")
datePickerDialog.setAccentColor(resources.getColor(R.color.colorPrimary))
datePickerDialog.setOkText("SIP")
datePickerDialog.setCancelText("GA JADI")
datePickerDialog.show(fragmentManager,"")
但是当我想在这一行datePickerDialog.show(fragmentManager,"")
显示日期选择器对话框时出现错误,就像这样
我相信我提供了正确的论点,但仍然......它给出了一个错误
使用 androidX,您应该使用最新版本:
implementation 'com.wdullaer:materialdatetimepicker:4.2.3'
在片段中:
fragmentManager?.let { manager ->
datePickerDialog.show(manager, "DatePickerDialog")
}
我不知道它是不是完美的方式我已经尝试过这段代码并且它正在工作但方法已被弃用。 如果您找到其他方法,请告诉我。
val now = Calendar.getInstance()
val currentYear: Int = now.get(Calendar.YEAR)
val currentMonth: Int = now.get(Calendar.MONTH)
val currentDay: Int = now.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog.newInstance(DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// do something here
}, currentYear, currentMonth, currentDay)
datePickerDialog.setTitle("INI JUDUL")
datePickerDialog.setAccentColor(resources.getColor(R.color.colorPrimary))
datePickerDialog.setOkText("SIP")
datePickerDialog.setCancelText("GA JADI")
datePickerDialog.show(activity!!.fragmentManager, "Datepickerdialog");
希望它会起作用!
刚试过这个,效果很好:
val now = Calendar.getInstance()
val currentYear: Int = now.get(Calendar.YEAR)
val currentMonth: Int = now.get(Calendar.MONTH)
val currentDay: Int = now.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog.newInstance(DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
// do something here
}, currentYear, currentMonth, currentDay)
datePickerDialog.setTitle("INI JUDUL")
datePickerDialog.setAccentColor(resources.getColor(R.color.colorPrimary))
datePickerDialog.setOkText("SIP")
datePickerDialog.setCancelText("GA JADI")
datePickerDialog.show(parentFragmentManager,"")
尝试清理项目并使缓存无效。 它可能会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.