带有数字月份而不是命名的 Spinner DatePicker

Spinner DatePicker with numeric months instead of named

我有一个基本的 DatePicker 微调器样式,没有任何布局。它看起来像这样:

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 以数字方式显示月份(两位数格式)? 换句话说,它会显示为 [=13= 而不是 Mar ].理想情况下,如果可能的话,我想避免必须实现自定义微调器。

您可以通过从 DatePicker 中找到月份 NumberPicker 并将显示的值设置为数字字符串来实现此目的。

进行以下更改:

1.Create 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.Use 下面的辅助函数在 DatePickerFragment 中将月份设置为数字,如下所示:

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
    }

结果: