Android 中AlertDialog警告对话框的使用

简介: Android 中AlertDialog警告对话框的使用

前言:AlertDialog 为警告对话框,可以实现多种样式。

分别为:

  1. 默认样式
  2. 单选样式
  3. 多选样式
  4. 自定义样式

接下来我会通过代码实现这些样式

首先实现布局页面:

activity_dialog.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"
    tools:context=".DialogActivity"
    android:orientation="vertical"
    >
    <Button
        android:id="@+id/btn_dialog1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AlertDialog样式1"
        />
    <Button
        android:id="@+id/btn_dialog2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AlertDialog样式2"
        />
    <Button
        android:id="@+id/btn_dialog3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AlertDialog样式3"
        />
    <Button
        android:id="@+id/btn_dialog4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AlertDialog样式4"
        />
    <Button
        android:id="@+id/btn_dialog5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AlertDialog样式5"
        />
</LinearLayout>

实现五种不同的样式AlertDialog

在DialogActivity 中实现,代码如下,具体详解已经在注释中给出

public class DialogActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_dialog1;
    private Button btn_dialog2;
    private Button btn_dialog3;
    private Button btn_dialog4;
    private Button btn_dialog5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        btn_dialog1 = findViewById(R.id.btn_dialog1);
        btn_dialog2 = findViewById(R.id.btn_dialog2);
        btn_dialog3 = findViewById(R.id.btn_dialog3);
        btn_dialog4 = findViewById(R.id.btn_dialog4);
        btn_dialog5 = findViewById(R.id.btn_dialog5);
        btn_dialog1.setOnClickListener(this);
        btn_dialog2.setOnClickListener(this);
        btn_dialog3.setOnClickListener(this);
        btn_dialog4.setOnClickListener(this);
        btn_dialog5.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_dialog1:
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("请选择").setMessage("你确定要退出吗?")
                        .setIcon(R.drawable.background) //设置图标
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(DialogActivity.this, "确定", Toast.LENGTH_SHORT).show();
                            }
                        }).setNeutralButton("我再想想", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, "我再想想", Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                }).show();
                break;
            case R.id.btn_dialog2:
                String[] array = new String[]{"男","女"};
                AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
                builder1.setTitle("选择性别").setSingleChoiceItems(array, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, array[which]+"", Toast.LENGTH_SHORT).show();
                    }
                }).show();
                break;
            case R.id.btn_dialog3:
                String[] array2 = new String[]{"男","女"};
                AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
                builder3.setTitle("选择性别").setSingleChoiceItems(array2, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, array2[which]+"", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();  //选中其中任何一项后,对话框消失
                    }
                }).setCancelable(false).show(); //setCancelable 如果没有选择任何一项,点击阴影部分,对话框不会消失
                break;
            case R.id.btn_dialog4:
                String[] array1 = new String[]{"读书","运动","旅游","写代码"};
                boolean[] isChecked = new boolean[]{true,false,false,false,false};
                AlertDialog.Builder builder2 = new AlertDialog.Builder(DialogActivity.this);
                builder2.setTitle("选择兴趣").setMultiChoiceItems(array1, isChecked, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        Toast.makeText(DialogActivity.this, array1[which]+":"+isChecked, Toast.LENGTH_SHORT).show();
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).show();
                break;
            case R.id.btn_dialog5:
                AlertDialog.Builder builder5 = new AlertDialog.Builder(DialogActivity.this);
                View view = LayoutInflater.from(this).inflate(R.layout.layout_dialog, null);
                EditText editUserName = view.findViewById(R.id.et_username);
                EditText editPassword = view.findViewById(R.id.et_password);
                Button button = view.findViewById(R.id.btn_login);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(DialogActivity.this, "用户名:"+editUserName.getText().toString()+",密码:"+editPassword.getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                });
                builder5.setTitle("请先登录!").setView(view)
                        .show();
                break;
            default:
                break;
        }
    }
}

效果图如图所示:


目录
相关文章
|
9月前
|
Android开发
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)
235 0
|
6月前
|
API Android开发
Android使用AlertDialog实现弹出菜单
本文分享了在Android开发中使用AlertDialog实现弹出菜单的方法,并通过代码示例和错误处理,展示了如何避免因资源ID找不到导致的crash问题。
98 1
|
8月前
|
Android开发 UED
|
8月前
|
API Android开发
32. 【Android教程】对话框:AlertDialog
32. 【Android教程】对话框:AlertDialog
137 2
|
7月前
|
API Android开发 UED
Android中使用AlertDialog创建对话框
Android中使用AlertDialog创建对话框
|
9月前
|
Android开发
Android之日期及时间选择对话框
Android之日期及时间选择对话框
49 1
|
Android开发
Android 中ProgressDialog进度条对话框的使用(使用子线程模拟更新进度)
Android 中ProgressDialog进度条对话框的使用(使用子线程模拟更新进度)
244 0
|
9月前
|
Android开发
[Android]AlertDialog对话框
[Android]AlertDialog对话框
103 0
|
XML Java Android开发
Android 对话框组件 AlertDialog 四种常用方法
Android 对话框组件 AlertDialog 四种常用方法
242 0
|
Android开发
android 自定义登陆对话框基类封装,且随着软键盘的弹起自动移动位置
android 自定义登陆对话框基类封装,且随着软键盘的弹起自动移动位置

热门文章

最新文章

  • 1
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 2
    Android历史版本与APK文件结构
  • 3
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 4
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
  • 5
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
  • 6
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 7
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 8
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 9
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 10
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
  • 1
    android FragmentManager 删除所有Fragment 重建
    15
  • 2
    Android实战经验之Kotlin中快速实现MVI架构
    21
  • 3
    即时通讯安全篇(一):正确地理解和使用Android端加密算法
    24
  • 4
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
    39
  • 5
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    122
  • 6
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    40
  • 7
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    55
  • 8
    Android历史版本与APK文件结构
    149
  • 9
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    46
  • 10
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    40