kotlin 具有数字月份而非名称月份的微调按钮DatePicker

e5nqia27  于 2022-11-16  发布在  Kotlin
关注(0)|答案(1)|浏览(152)

我有一个基本的DatePicker,它是spinner风格的,没有任何布局。它看起来像这样:

class DatePickerFragment: DialogFragment(), DatePickerDialog.OnDateSetListener {

    private lateinit var date: Date

    interface Callbacks {
        fun onDateSelected(date: Date)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        date = arguments?.getSerializable(ARG_DATE) as Date
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val calendar = Calendar.getInstance().apply {
            time = date
        }

        val initialYear = calendar.get(Calendar.YEAR)
        val initialMonth = calendar.get(Calendar.MONTH)
        val initialDay = calendar.get(Calendar.DAY_OF_MONTH)

        return DatePickerDialog(
            requireContext(),
            this,
            initialYear,
            initialMonth,
            initialDay
        )
    }

    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        val selectedDate = GregorianCalendar(year, month, dayOfMonth).time
        targetFragment?.let { fragment ->
            (fragment as Callbacks).onDateSelected(selectedDate)
        }
    }

    companion object {
        fun getInstance(date: Date): DatePickerFragment {
            return DatePickerFragment().apply {
                arguments = Bundle().apply {
                    putSerializable(ARG_DATE, date)
                }
            }
        }
    }
}

private const val ARG_DATE = "date"

选择器正常工作。问题是它显示月份名称(三个字母的格式),请参阅:

**如何使DatePicker以数字形式显示月份(两位数格式)?**换句话说,它将显示为Mar而不是03。理想情况下,如果可能,我希望避免实现自定义微调器。

h22fl7wq

h22fl7wq1#

通过从DatePicker中查找月份NumberPicker并将显示的值设置为数字字符串,可以实现此目的。
进行以下更改:
1.使用如下自定义样式在onCreateDialog(savedInstanceState: Bundle?)中创建DatePickerDialog:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val calendar = Calendar.getInstance().apply {
            time = date
        }

        val initialYear = calendar.get(Calendar.YEAR)
        val initialMonth = calendar.get(Calendar.MONTH)
        val initialDay = calendar.get(Calendar.DAY_OF_MONTH)

        val mPickerDialog: DatePickerDialog = object : DatePickerDialog(requireContext(), R.style.MyDatePickerDialogStyle, this, initialYear, initialMonth, initialDay) {
            override fun onDateChanged(view: DatePicker, year: Int, month: Int, dayOfMonth: Int) {
                super.onDateChanged(view, year, month, dayOfMonth)
                setNumericMonth(view)
            }
        }
        setNumericMonth(mPickerDialog.datePicker)
        return mPickerDialog
    }

其中R.style.MyDatePickerDialogStyle是自定义样式,用于设置微调器样式,如下所示:

<style name="MyDatePickerDialogStyle" parent="android:Theme.Material.Dialog">
      <item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
</style>
    
<style name="MyDatePickerStyle" parent="android:Widget.Material.DatePicker">
     <item name="android:datePickerMode">spinner</item>
 </style>

2.使用DatePickerFragment中的以下帮助函数将month设置为数字,如下所示:

private fun setNumericMonth(datePicker: DatePicker) {
        val monthNumbers = arrayOf("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
        val monthPicker = getMonthNumberPicker(datePicker)
        if (monthPicker != null) {
            monthPicker.displayedValues = monthNumbers
        }
    }

private fun getMonthNumberPicker(datePicker: DatePicker?): NumberPicker? {
        try {
            if (datePicker != null && datePicker.childCount > 0 && datePicker.getChildAt(0) is ViewGroup) {
                val vg = datePicker.getChildAt(0) as ViewGroup
                if (vg.childCount > 0 && vg.getChildAt(0) is ViewGroup) {
                    val vgPickers = vg.getChildAt(0) as ViewGroup
                    for (i in 0 until vgPickers.childCount) {
                        if (vgPickers.getChildAt(i) is NumberPicker && i == 1) {
                            return vgPickers.getChildAt(i) as NumberPicker
                        }
                    }
                }
            }
        } catch (e: Exception) {
        }
        return null
    }

结果:

相关问题