如何使用Kotlin在片段中设置日期选择器?

问题描述 投票:0回答:1

我想使用此库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,"")中显示该日期选择器对话框时出现错误,就像这样

enter image description here

我相信我提供了正确的论点,但仍然..它给出了一个错误

这是片段管理器类型:enter image description here

但是如果我强制这样展开,它仍然会出错enter image description here

android kotlin android-datepicker
1个回答
0
投票
我不知道它是不是完美的方法,我是否尝试过此代码,它可以工作,但是不建议使用方法。如果您找到其他方法,请告诉我。

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");

希望它能起作用!
© www.soinside.com 2019 - 2024. All rights reserved.