繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM