3.28

所花时间(包括上课):2

打码量(行):350

博客量(篇):1

了解到知识点:学习时间对话框TimePickerDialog

 

 import android.app.TimePickerDialog;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TimePicker;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;

 

public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {

 

    private Button showTimePickerButton;

    private Calendar calendar;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        showTimePickerButton = findViewById(R.id.btn_show_time_picker);

        calendar = Calendar.getInstance();

 

        showTimePickerButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                int hour = calendar.get(Calendar.HOUR_OF_DAY);

                int minute = calendar.get(Calendar.MINUTE);

 

                // 创建一个新的 TimePickerDialog 对象

                TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,

                        MainActivity.this, hour, minute, true);

 

                // 显示时间选择器对话框

                timePickerDialog.show();

            }

        });

    }

 

    @Override

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        // 当用户选择时间时触发该方法

        String selectedTime = hourOfDay + ":" + minute;

        Toast.makeText(MainActivity.this, "Selected time: " + selectedTime, Toast.LENGTH_SHORT).show();

    }

}

<!-- activity_main.xml -->

<Button

    android:id="@+id/btn_show_time_picker"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Show Time Picker" />

posted @ 2024-03-28 21:42  赵千万  阅读(5)  评论(0编辑  收藏  举报