ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android中常用Dialog(弹窗)

2021-10-05 16:06:28  阅读:290  来源: 互联网

标签:Toast show int void Dialog DialogDemo Android progressDialog 弹窗


Android中常用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();

    }

}

标签:Toast,show,int,void,Dialog,DialogDemo,Android,progressDialog,弹窗
来源: https://blog.csdn.net/Carryi/article/details/120614830

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有