Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

android - How to pick time using material design?

how to pick a time using material:1.3.0-alpha01, I am looking time picker following screenshot, which I found in material io.

I tried date picker is working material time picker

Material Date picker

//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

Simple Time picker

  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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the new MaterialTimePicker introduced with the Material Components Library.

Note: this code requires at least the version 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
}

enter image description here

Using the same code with:

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

materialTimePicker.show(supportFragmentManager, "fragment_tag")

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...