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;
        }
    }
}

效果图如图所示:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值