This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

background

The last article mentioned that the project required a year, month, and day selection box. Since the three-party library PickerView did not meet the requirements, this article will introduce another Google control TimePicker

The effect

integration

1. Insert a TimePicker into the XML

<TimePicker
    android:layout_marginTop="20dp"
    android:id="@+id/time_picker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFFFF"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tv_date"/>
Copy the code

2. Initialize and invoke in the code

// Whether to display the 24 hour view
timePicker.setIs24HourView(false);
// Set when selected
timePicker.setCurrentHour(2);
// Set the selected points
timePicker.setCurrentMinute(59);
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        Log.e("onTimeChanged",hourOfDay+""+minute);
        Calendar timeCalendar = Calendar.getInstance();
        timeCalendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
        timeCalendar.set(Calendar.MINUTE,minute);
        time.setText(android.text.format.DateFormat.getTimeFormat(getBaseContext()).format(newDate(timeCalendar.getTimeInMillis()))); }});Copy the code

The effect is as follows, and cannot meet the demand

Style adjustment

With that experience in mind, go straight to the document and find a similar property android:timePickerMode

Defines the look of the widget. Prior to the L release, the only choice was spinner. As of L, with the Material theme selected, the default layout is clock, but this attribute can be used to force spinner to be used instead.

Must be one of the following constant values.

Android :timePickerMode must be set to one of two values if you want to change the TimePicker style

field value Introduction to the
clock 2 Time picker with clock face to select the time.
spinner 1 Time picker with spinner controls to select the time.

The Settings look like this

The Ui is almost complete

Format the time format according to different regions

The method I’m using in my project right now is android.text.format. Method in the DateFormat class under the package

android.text.format.DateFormat.getTimeFormat(getBaseContext()).format(new Date(timeCalendar.getTimeInMillis()))
Copy the code

TimePicker common properties

Android :datePickerMode: control display mode clock or spinner. GetCurrentHour (): get the current selected time, if minSdkVersion >=23 recommended to use getHour() GetCurrentMinute (): Obtains the selected minute. If minSdkVersion is >=23, you are advised to use getMinute() setCurrentHour(): obtains the selected minute. If minSdkVersion is >=23, you are advised to use getMinute() setCurrentHour(): obtains the selected minute >=23 setHour() setCurrentMinute() is recommended to obtain the selected minute. If minSdkVersion >=23 setMinute() is24HourView() is recommended to obtain whether the current component is in 24-hour mode SetIs24HourView (): Sets whether the current component is in 24-hour mode

The project address

The project address