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();

    }

}

到此這篇關於Android中常用的三個Dialog彈窗總結解析的文章就介紹到這瞭,更多相關Android Dialog內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: