Android——AlertDialog提示对话框的使用

【作用】:AlertDialog可以在当前界面弹出一个对话框,置于所有界面之上,可以用于提示重要内容或者警告信息,比如关键开关的关闭,重要内容的删除等操作时,加入此控件提示用户谨慎操作;

【代码示例】:

AlertDialog.Builder dialog = new AlertDialog.Builder(mainActivity);
        dialog.setTitle("温馨提示");//设置标题
        dialog.setMessage("关闭后将无法启动xxxx为您服务,确定要关闭吗?");//设置内容
        dialog.setCancelable(false);//back键是否可以关闭对话框
        //设置确定按钮点击事件
        dialog.setPositiveButton("仍然关闭", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                    //加入自己的相关操作
                }
        });
        //设置取消按钮点击事件
        dialog.setNegativeButton("保持开启", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                   //加入自己的相关操作
                }
        });
        dialog.show();
发布了86 篇原创文章 · 获赞 53 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/w464960660/article/details/102802656