场景

Android中常用的五种对话框为

常规对话框、带列表的对话框、自定义的对话框、带进度条的对话框、带日期选择器的对话框。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

常规对话框

新建一个空项目,调整activity_main.xml的布局为LinearLayout,并添加一个Button,设置其点击事件

    <Button
        android:id="@+id/button_normal_dialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startNormalDialog"
        android:text="启动常规对话框" />

然后在activity中实现其点击方法

    /***
     * 启动常规对话框
     * @param view
     */
    public void startNormalDialog(View view){
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.ic_launcher_background)
                .setTitle("公众号:霸道的程序猿")
                .setCancelable(false)
                .setMessage("是否删除该条记录")
                .setPositiveButton("", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"删除记录成功",Toast.LENGTH_LONG).show();
                    }
                })
                .setNegativeButton("", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"取消删除记录",Toast.LENGTH_LONG).show();
                    }
                })
                .show();
    }

运行项目查看效果

 

 

带列表的对话框

在布局文件中添加一个按钮

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startListDialog"
        android:text="启动带列表的对话框" />

 

在activity中实现其点击方法

 

    /**
     * 启动带列表的对话框
     * @param view
     */
    public void startListDialog(View view){
                final String[] items = {"关注","公众号","霸道的程序猿","获取","编程教程与资源"};
                new AlertDialog.Builder(this)
                        .setTitle("公众号:霸道的程序猿")
                        .setCancelable(false)
                        .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_LONG).show();
                            }
                        })
                        .setPositiveButton("OK",null)
                        .show(); 

}

 

运行之后查看效果

 

 

自定义视图的对话框

在layout下新建layout资源文件dialog_view.xml

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

    <EditText
        android:id="@+id/text_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="用户名"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/text_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="密码"
        android:inputType="textPersonName" />
</androidx.appcompat.widget.LinearLayoutCompat>

 

然后在主布局文件中添加一个按钮

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startCustomDialog"
        android:text="启动自定义的对话框" />

 

在activity中实现其点击方法

    /**
     * 启动自定义的对话框
     * @param view
     */
    public void startCustomDialog(View view){
                View v = View.inflate(this,R.layout.dialog_view,null);
                final EditText user = v.findViewById(R.id.text_username);
                final EditText pwd = v.findViewById(R.id.text_pwd);

                new AlertDialog.Builder(this)
                        .setTitle("公众号:霸道的程序猿")
                        .setCancelable(false)
                        .setView(v)
                        .setPositiveButton("登录", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String username = user.getText().toString().trim();
                                String password = pwd.getText().toString().trim();
                                Toast.makeText(MainActivity.this,"用户名:"+username+"密码:"+password,Toast.LENGTH_LONG).show();
                            }
                        })
                        .show();
    }

 

运行之后查看效果

 

 

带进度条的对话框

在布局文件中添加按钮

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startProgressDialog"
        android:text="启动带进度条的对话框" />

 

在activity中实现其点击方法

    /**
     * 启动带进度条的对话框
     * @param view
     */
    public void startProgressDialog(View view){
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("公众号:霸道的程序猿");
        progressDialog.setCancelable(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();

        new Thread(new Runnable() {
            @Override
            public void run() {
               for(inti=1;i<=20;i++){
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progressDialog.setProgress(progressDialog.getProgress()+5);
                }
                progressDialog.dismiss();
            }
        }).start();
    }

 

运行之后查看效果

 

 

带日期选择器的对话框

在布局文件中添加按钮

    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startDatePickDialog"
        android:text="启动带日期选择器的对话框" />

 

在activity中实现其点击方法

 

   /**
     * 启动带日期选择器的对话框
     * @param view
     */
    public void startDatePickDialog(View view){
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this,"选中了"+year+""+month+""+dayOfMonth+"",Toast.LENGTH_LONG).show();
            }
        },year,month,day).show();
    }

 

运行之后查看效果

activity_main.xml完整代码

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_normal_dialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startNormalDialog"
        android:text="启动常规对话框" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startListDialog"
        android:text="启动带列表的对话框" />


    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startCustomDialog"
        android:text="启动自定义的对话框" />


    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startProgressDialog"
        android:text="启动带进度条的对话框" />


    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startDatePickDialog"
        android:text="启动带日期选择器的对话框" />
</LinearLayout>

 

MainActivity完整代码

package com.badao.androidstudy;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

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

    /***
     * 启动常规对话框
     * @param view
     */
    public void startNormalDialog(View view){
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.ic_launcher_background)
                .setTitle("公众号:霸道的程序猿")
                .setCancelable(false)
                .setMessage("是否删除该条记录")
                .setPositiveButton("", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"删除记录成功",Toast.LENGTH_LONG).show();
                    }
                })
                .setNegativeButton("", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"取消删除记录",Toast.LENGTH_LONG).show();
                    }
                })
                .show();
    }

    /**
     * 启动带列表的对话框
     * @param view
     */
    public void startListDialog(View view){
                final String[] items = {"关注","公众号","霸道的程序猿","获取","编程教程与资源"};
                new AlertDialog.Builder(this)
                        .setTitle("公众号:霸道的程序猿")
                        .setCancelable(false)
                        .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_LONG).show();
                            }
                        })
                        .setPositiveButton("OK",null)
                        .show();
    }

    /**
     * 启动自定义的对话框
     * @param view
     */
    public void startCustomDialog(View view){
                View v = View.inflate(this,R.layout.dialog_view,null);
                final EditText user = v.findViewById(R.id.text_username);
                final EditText pwd = v.findViewById(R.id.text_pwd);

                new AlertDialog.Builder(this)
                        .setTitle("公众号:霸道的程序猿")
                        .setCancelable(false)
                        .setView(v)
                        .setPositiveButton("登录", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String username = user.getText().toString().trim();
                                String password = pwd.getText().toString().trim();
                                Toast.makeText(MainActivity.this,"用户名:"+username+"密码:"+password,Toast.LENGTH_LONG).show();
                            }
                        })
                        .show();
    }


    /**
     * 启动带进度条的对话框
     * @param view
     */
    public void startProgressDialog(View view){
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("公众号:霸道的程序猿");
        progressDialog.setCancelable(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=1;i<=20;i++){
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progressDialog.setProgress(progressDialog.getProgress()+5);
                }
                progressDialog.dismiss();
            }
        }).start();
    }


    /**
     * 启动带日期选择器的对话框
     * @param view
     */
    public void startDatePickDialog(View view){
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this,"选中了"+year+""+month+""+dayOfMonth+"",Toast.LENGTH_LONG).show();
            }
        },year,month,day).show();
    }

 

}

 

posted on 2020-11-30 22:05  霸道流氓  阅读(827)  评论(0编辑  收藏  举报