美文网首页
AlertDialog.Builder介绍

AlertDialog.Builder介绍

作者: b_walking | 来源:发表于2024-01-05 19:52 被阅读0次
  1. 链式写法
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题")
        .setMessage("内容")
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                        // todo
                }
        })
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                        // todo
                }
        })
        .setCancelable(false) // 此句的作用是使得弹框只通过点击确认或取消按钮关闭,不能通过点击弹框外部区域关闭
        .create().show();
  1. 修改弹框背景透明度
AlertDialog dialog = builder.create();
dialog.show();
dialog.getWindow().getAttributes().windowAnimations = R.style.MyDialogAnimation;
dialog.getWindow().setDimAmount(0f); // 0为全透明,1为全黑
  1. 弹框动画
    弹框在出现和消失时均有一个动画,这个动画的呈现方式和持续时间都可以自行设置。

相关文章

网友评论

      本文标题:AlertDialog.Builder介绍

      本文链接:https://www.haomeiwen.com/subject/oguindtx.html