如何关闭自定义警报对话框

时间:2018-03-18 15:30:01

标签: android android-studio dialog alertdialog

我有一个自定义提醒对话框,我有一个imageView用于关闭对话框,但我不知道如何做到这一点。 我尝试通过解雇或取消来做,但它不起作用

        public void onClick(View v)
        {

            final AlertDialog.Builder dialog = new AlertDialog.Builder(context);

            final View viewLayout = mInflater.inflate(R.layout.listview_addstudent_items , null);

            dialog.setView(viewLayout);
            ImageView closeIcon_img = (ImageView)viewLayout.findViewById(R.id.item_img_close);

            closeIcon_img.setOnClickListener(new View.OnClickListener() {

             // I want To dismiss Here

            });

            dialog.show();

        }
    });

2 个答案:

答案 0 :(得分:1)

尝试像这样创建AlertDialog表单AlertDialog.Builder

 AlertDialog.Builder dialog = new AlertDialog.Builder(context);
    final View viewLayout = mInflater.inflate(R.layout.listview_addstudent_items , null);
    dialog.setView(viewLayout);
    ImageView closeIcon_img = (ImageView)viewLayout.findViewById(R.id.item_img_close);
    final AlertDialog  alertDialog=dialog.create();
    closeIcon_img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialog.dismiss();
        }
    });
    alertDialog.show();

答案 1 :(得分:0)

试试这个:

AlertDialog dialog = builder.create();
closeIcon_img.setOnClickListener(new View.OnClickListener() {

             dialog.dismiss();

});
dialog.show();