Closing a custom alert dialog on button click

前端 未结 10 1216
灰色年华
灰色年华 2020-12-06 04:01

I\'m having trouble closing my alert dialog. I am using a layout inflator to make the dialog, so I\'m not sure how I would go about closing the thing after I\'m done with it

10条回答
  •  醉梦人生
    2020-12-06 04:51

    use this code in your class:

    Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.custom);
    ((Button) dialog.findViewById(R.id.button))
       .setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    dialog.show();
    

    and create custom.xml, then paste this code inside it:

    
    
        

    source: https://www.mkyong.com/android/android-custom-dialog-example/

    and if you do not like above code, so see below link: http://thedeveloperworldisyours.com/android/prevent-alertdialog-closing-button-clicked/

提交回复
热议问题