如何使用material设计来挑选时间?

How to pick time using material design?

如何使用 material:1.3.0-alpha01 选择时间,我正在寻找时间选择器,我在 material io.

中找到了截图

我试过日期选择器可以正常工作

Material 日期选择器

//Creating a MaterialDatePicker.Builder instance that gives us a single date selector.
val builder : MaterialDatePicker.Builder<*> = MaterialDatePicker.Builder.datePicker() // 1
//Building-up the MaterialDatePicker by calling a builder.build function
val picker : MaterialDatePicker<*> = builder.build()  // 2
//Display the Material Date Picker.
picker.show(requireActivity().supportFragmentManager, picker.toString())   // 3

简单的时间选择器

  val cal = Calendar.getInstance()
            val timeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute ->
                cal.set(Calendar.HOUR_OF_DAY, hour)
                cal.set(Calendar.MINUTE, minute)
                //textView.text = SimpleDateFormat("HH:mm").format(cal.time)
            }
            val time = TimePickerDialog(context, timeSetListener, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true)
            time.setTitle("")
            time.show()

您可以使用随 Material 组件库引入的新 MaterialTimePicker

注意:此代码需要至少版本1.3.0-alpha03

import com.google.android.material.timepicker.MaterialTimePicker

val materialTimePicker = MaterialTimePicker.Builder()
    .setTimeFormat(TimeFormat.CLOCK_24H)
    .build()

materialTimePicker.addOnPositiveButtonClickListener {
    val newHour: Int = materialTimePicker.hour
    val newMinute: Int = materialTimePicker.minute
}

使用相同的代码:

val materialTimePicker = MaterialTimePicker.Builder()
    .setTimeFormat(TimeFormat.CLOCK_12H)
    .build()

materialTimePicker.show(supportFragmentManager, "fragment_tag")

要使用 MATERIAL TIME PICKER,请将以下依赖项添加到项目的 build.gradle 文件中:

   implementation 'com.google.android.material:material:1.3.0-alpha02'

Kotlin 时间选择器使用 material:1.3.0-alpha02

val timePickerDialog = MaterialTimePicker.newInstance();
            timePickerDialog.show(requireActivity().supportFragmentManager, "fragment_tag");
            // Types of formats for the time pickeer
            timePickerDialog.setTimeFormat(TimeFormat.CLOCK_12H)//CLOCK_24H
            timePickerDialog.setListener { dialog: MaterialTimePicker ->
                val newHour = dialog.hour
                val newMinute = dialog.minute
}