How to dismiss Custom Alert Dialog

2019-07-23 22:02发布

i have a Custom Alert Dialog , i have a imageView For close Dialog but i don`t know how to do it. I try to do it by dismiss or cancel, but it does not work

        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条回答
Ridiculous、
2楼-- · 2019-07-23 22:53

Try this :

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

             dialog.dismiss();

});
dialog.show();
查看更多
老娘就宠你
3楼-- · 2019-07-23 22:57

Try Like this create AlertDialog form 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();
查看更多
登录 后发表回答