Android: TimePicker trong Android


Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên

TimePicker trong Android cho phép bạn lựa chọn thời gian của ngày trong chế độ hoặc 24 h hoặc AM/PM. Thời gian bao gồm các định dạng hour, minute, và clock. Android cung cấp tính năng này thông qua lớp TimePicker.

Để sử dụng lớp TimePicker, đầu tiên bạn phải định nghĩa thành phần TimePicker trong activity.xml của bạn. Định nghĩa như sau:

<TimePicker
   android:id="@+id/timePicker1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

Sau đó, bạn phải tạo một đối tượng của lớp TimePicker và nhận tham chiếu của thành phần xml đã định nghĩa ở trên. Cú pháp là:

import android.widget.TimePicker;
private TimePicker timePicker1;
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);

Để lấy thời gian đã được chọn bởi người dùng trên màn hình, bạn sẽ sử dụng phương thức getCurrentHour() và getCurrentMinute() của lớp TimePicker. Cú pháp của chúng là:

int hour = timePicker1.getCurrentHour();
int min = timePicker1.getCurrentMinute();

Ngoài các phương thức này, còn có một số phương thức khác trong API để cung cấp nhiều điều khiển hơn qua thành phần TimePicker. Bảng dưới liệt kê các phương thức này:

Stt Phương thức & Miêu tả
1 is24HourView()

Phương thức này trả về true nếu trong 24h, nếu không là false

2 isEnabled()

Phương thức này trả về trạng thái kích hoạt cho view này

3 setCurrentHour(Integer currentHour)

Phương thức này thiết lập giờ hiện tại

4 setCurrentMinute(Integer currentMinute)

Phương thức này thiết lập phút hiện tại

5 setEnabled(boolean enabled)

TPhương thức này thiết lập trạng thái kích hoạt cho view này

6 setIs24HourView(Boolean is24HourView)

Phương thức này thiết lập có hay không trong chế độ 24 h hoặc chế độ AM/PM

7 setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)

Phương thức này thiết lập hàm callback mà chỉ rằng thời gian đã được chỉnh sửa bởi người dùng

Ví dụ áp dụng

Dưới đây là các bước chi tiết để tạo ứng dụng TimePicker trong Android.

Bạn sửa đổi nội dung của activity_main.xml như sau.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textColor="#8BC34A"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.023" />

    <TimePicker
        android:id="@+id/timePicker"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.28" />

    <Button
        android:id="@+id/btnGetTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showTime"
        android:text="@string/get_time"
        android:textColor="#8BC34A"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.76" />

    <TextView
        android:id="@+id/txtShowTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#8BC34A"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.913" />

</androidx.constraintlayout.widget.ConstraintLayout>

Sau đây là nội dung của file MainActivity.java:

package v1study.com.timepicker;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void showTime(View view){
        TimePicker timePicker=(TimePicker)findViewById(R.id.timePicker);
        TextView textView=(TextView)findViewById(R.id.txtShowTime);
        int h=timePicker.getCurrentHour();
        String hour=""+h;
        String format="AM";
        if(h<10){
            hour="0"+h;
        }
        if(h==12){
            format="PM";
        }
        if(h>12){
            format="PM";
            hour="0"+(h-12);
        }
        textView.setText(hour+":"+timePicker.getCurrentMinute().toString()+" "+format);
    }
}

Và đây là nội dung của strings.xml.

<resources>
    <string name="app_name">TimePickerV1Study</string>
    <string name="get_time">Get time</string>
</resources>

Chạy ứng dụng:

TimePicker trong Android

Nhấn nút Get Time để được kết quả:

TimePicker

» Tiếp: DatePicker trong Android
« Trước: Spinner trong Android
Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên
Copied !!!