一手遮天 Android - view(弹出类): TimePickerDialog 基础

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(弹出类): TimePickerDialog 基础

示例如下:

/view/flyout/TimePickerDialogDemo1.java

/**
 * TimePickerDialog - 时间选择对话框
 *
 * 注1:TimePickerDialog 继承自 AlertDialog
 * 注2:实际项目中建议使用第三方的时间选择控件
 */

package com.webabcd.androiddemo.view.flyout;

import android.app.TimePickerDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

import com.webabcd.androiddemo.R;

import java.util.Calendar;

public class TimePickerDialogDemo1 extends AppCompatActivity {

    private Button mButton1;

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

        mButton1 = findViewById(R.id.button1);

        sample();
    }

    private void sample() {
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                TimePickerDialog timePickerDialog = new TimePickerDialog(TimePickerDialogDemo1.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        // 获取用户选择的时间
                        Toast.makeText(getApplicationContext(), String.format("%d:%d", hourOfDay, minute), Toast.LENGTH_SHORT).show();
                    }
                },
                        calendar.get(Calendar.HOUR_OF_DAY),
                        calendar.get(Calendar.MINUTE), true);

                timePickerDialog.show();
            }
        });
    }
}

/layout/activity_view_flyout_timepickerdialogdemo1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="弹出时间选择对话框" />

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-05-31 13:20  webabcd  阅读(126)  评论(0编辑  收藏  举报