Android中常用的三个Dialog弹窗总结解析

 更新时间:2021年10月26日 10:55:18   作者:青素i  
自己虽然一直使用过dialog,但是一直都是复制、粘贴;不清楚dialog的具体用途,这次趁着有时间,总结一下具体用法,感兴趣的朋友跟着小编来看看吧

ProgressDialog

    private  void showProgressDialog(){
        progressDialog = new ProgressDialog(DialogDemo.this);

        //设置提示信息
        progressDialog.setTitle("提示");
        progressDialog.setIcon(R.mipmap.touxiang0);

        progressDialog.setMessage("正在处理中");

        //是否用过返回键取消
        progressDialog.setCancelable(true);
        //碰触弹框之外的地方取消
        progressDialog.setCanceledOnTouchOutside(true);


        //显示
        progressDialog.show();
    }

在这里插入图片描述

DatePickerDialog

 //日期
    private  void datePickerDialog(){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            DatePickerDialog datePickerDialog = new DatePickerDialog(DialogDemo.this);
            datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    Toast.makeText(DialogDemo.this,year+"年"+(month+1)+"月"+dayOfMonth+"日",Toast.LENGTH_SHORT).show();
                }
            });
            datePickerDialog.show();
        }else {
            Toast.makeText(DialogDemo.this,"版本过低",Toast.LENGTH_SHORT).show();

        }
    }

在这里插入图片描述

TimePickerDialog

    //时间
    private  void timePickerDialog(){
        //获得日历的实列
        Calendar calendar = Calendar.getInstance();

        //设置当前时间
        calendar.setTimeInMillis(System.currentTimeMillis());

        //获取时分

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


        //第三、四个参数初始时分 第五个参数是否为24小时显示
        TimePickerDialog time = new TimePickerDialog(DialogDemo.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(DialogDemo.this,"Hour"+hourOfDay+"minute"+minute,Toast.LENGTH_SHORT).show();

            }
        },hour,minute,true);

        time.show();

    }

在这里插入图片描述

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center_horizontal"

    >


    <Button
        android:id="@+id/btnProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:backgroundTint="#64D7E6"
        android:text="提示"
        android:textSize="35sp"

        />
    <Button
        android:id="@+id/btnDatePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:backgroundTint="#64D7E6"
        android:text="日期"
        android:textSize="35sp"

        />
    <Button
        android:id="@+id/btnTimePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:backgroundTint="#64D7E6"
        android:text="时间"
        android:textSize="35sp"
        />

</LinearLayout>

完整代码

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class DialogDemo extends AppCompatActivity {
    Button mBtnProgress,mBtnDatePicker,mBtnTimePicker;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_demo);
        initView();
        MyOnClick myOnClick = new MyOnClick();
        mBtnProgress.setOnClickListener(myOnClick);
        mBtnDatePicker.setOnClickListener(myOnClick);
        mBtnTimePicker.setOnClickListener(myOnClick);
    }

    private  void initView(){
        mBtnProgress = findViewById(R.id.btnProgress);
        mBtnDatePicker = findViewById(R.id.btnDatePicker);
        mBtnTimePicker = findViewById(R.id.btnTimePicker);
    }


    class MyOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case  R.id.btnProgress:
                    showProgressDialog();
                    break;
                case  R.id.btnDatePicker:
                    datePickerDialog();
                    break;
                case  R.id.btnTimePicker:
                    timePickerDialog();
                    break;
            }
        }
    }

    private  void showProgressDialog(){
        progressDialog = new ProgressDialog(DialogDemo.this);

        //设置提示信息
        progressDialog.setTitle("提示");
        progressDialog.setIcon(R.mipmap.touxiang0);

        progressDialog.setMessage("正在处理中");

        //是否用过返回键取消
        progressDialog.setCancelable(true);
        //碰触弹框之外的地方取消
        progressDialog.setCanceledOnTouchOutside(true);


        //显示
        progressDialog.show();
    }

    //日期
    private  void datePickerDialog(){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            DatePickerDialog datePickerDialog = new DatePickerDialog(DialogDemo.this);
            datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    Toast.makeText(DialogDemo.this,year+"年"+(month+1)+"月"+dayOfMonth+"日",Toast.LENGTH_SHORT).show();
                }
            });
            datePickerDialog.show();
        }else {
            Toast.makeText(DialogDemo.this,"版本过低",Toast.LENGTH_SHORT).show();

        }
    }


    //时间
    private  void timePickerDialog(){
        //获得日历的实列
        Calendar calendar = Calendar.getInstance();

        //设置当前时间
        calendar.setTimeInMillis(System.currentTimeMillis());

        //获取时分

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


        //第三、四个参数初始时分 第五个参数是否为24小时显示
        TimePickerDialog time = new TimePickerDialog(DialogDemo.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(DialogDemo.this,"Hour"+hourOfDay+"minute"+minute,Toast.LENGTH_SHORT).show();

            }
        },hour,minute,true);

        time.show();

    }

}

到此这篇关于Android中常用的三个Dialog弹窗总结解析的文章就介绍到这了,更多相关Android Dialog内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • android textview 显示html方法解析

    android textview 显示html方法解析

    现在网络的繁盛时代,光文字是不能满足人们的胃口的,图片,flash,音频,视频就成为浏览网页的主流显示,在手机上也一样,本文将详细介绍此功能的实现方法
    2012-11-11
  • Android图片处理工具类BitmapUtils

    Android图片处理工具类BitmapUtils

    这篇文章主要为大家详细介绍了Android图片的处理工具类BitmapUtils,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android开发导入项目报错Ignoring InnerClasses attribute for an anonymous inner class的解决办法

    Android开发导入项目报错Ignoring InnerClasses attribute for an anonym

    今天小编就为大家分享一篇关于Android开发导入项目报错Ignoring InnerClasses attribute for an anonymous inner class的解决办法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Android实现日夜间模式的深入理解

    Android实现日夜间模式的深入理解

    相信Android的日间/夜间模式切换相信大家在平时使用 APP 的过程中都遇到过,比如知乎、简书中就有相关的模式切换。实现日间/夜间模式切换的方案也有许多种,趁着今天有空来讲一下日间/夜间模式切换的几种实现方案,也可以做一个横向的对比来看看哪种方案最好。
    2016-09-09
  • Android中获取控件宽高的4种方法集合

    Android中获取控件宽高的4种方法集合

    下面小编就为大家分享一篇Android中获取控件宽高的4种方法集合,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-02-02
  • Android Studio 3.6中使用视图绑定替代 findViewById的方法

    Android Studio 3.6中使用视图绑定替代 findViewById的方法

    从 Android Studio 3.6 开始,视图绑定能够通过生成绑定对象来替代 findViewById,从而可以帮您简化代码、移除 bug,并且从 findViewById 的模版代码中解脱出来,今天通过本文给大家介绍使用视图绑定替代 findViewById的方法,感兴趣的朋友一起看看吧
    2020-03-03
  • Android实现在一个activity中添加多个listview的方法

    Android实现在一个activity中添加多个listview的方法

    这篇文章主要介绍了Android实现在一个activity中添加多个listview的方法,分析了Activity中添加listview的原理与具体实现方法,需要的朋友可以参考下
    2016-08-08
  • android中实现完全退出程序方法(退出所有activity)

    android中实现完全退出程序方法(退出所有activity)

    这篇文章主要介绍了android中实现完全退出程序方法(退出所有activity),本文方法是博主个人使用的一个方法,据说效果非常好,需要的朋友可以参考下
    2015-05-05
  • Android MaterialCardView的使用介绍与示例

    Android MaterialCardView的使用介绍与示例

    MaterialCardView是一个基于Android支持库中的CardView的可自定义组件。 MaterialCardView提供了CardView的所有功能,但增加了一些自定义属性,使用起来更加方便实用
    2021-11-11
  • Android自定义输入框提示功能

    Android自定义输入框提示功能

    这篇文章主要为大家详细介绍了Android自定义输入框提示功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09

最新评论