private fun initTimePicker(picker: TimePicker) {
val systemResources: Resources = Resources.getSystem()
val hourNumberPickerId: Int = systemResources.getIdentifier("hour", "id", "android")
val minuteNumberPickerId: Int = systemResources.getIdentifier("minute", "id", "android")
val dividerId = systemResources.getIdentifier("divider", "id", "android") //中间的冒号
val hourNumberPicker =
picker.findViewById<NumberPicker>(hourNumberPickerId)
val minuteNumberPicker =
picker.findViewById<NumberPicker>(minuteNumberPickerId)
val endDivider: TextView = picker.findViewById(dividerId)
setNumberPickerDivider(hourNumberPicker)
setNumberPickerDivider(minuteNumberPicker)
endDivider.text = ""
}
private fun setNumberPickerDivider(numberPicker: NumberPicker) {
val pickerFields: Array<Field> = NumberPicker::class.java.declaredFields
for (pf in pickerFields) {
if (pf.name.equals("mSelectionDivider")) { //设置颜色
pf.isAccessible = true
val colorDrawable = ColorDrawable(
ContextCompat.getColor(requireContext(), R.color.transparent)
) //选择自己喜欢的颜色
try {
pf.set(numberPicker, colorDrawable)
} catch (e: IllegalAccessException) {
e.printStackTrace()
}
}
numberPicker.invalidate()
}
}
Android TimePicker去掉分割线冒号
最新推荐文章于 2024-07-24 11:53:43 发布