问题
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 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()
回答1:
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
}
Using the same code with:
val materialTimePicker = MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_12H)
.build()
materialTimePicker.show(supportFragmentManager, "fragment_tag")
回答2:
To use MATERIAL TIME PICKER, add the following dependency to your project's build.gradle file:
implementation 'com.google.android.material:material:1.3.0-alpha02'
Kotlin time picker using 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
}
来源:https://stackoverflow.com/questions/62882368/how-to-pick-time-using-material-design