How to click 'OK' on an AlertDialog via code?

前端 未结 4 1492
执念已碎
执念已碎 2021-01-17 12:35

I use showDialog and dismissDialog in activity to display and destroy my dialog. Is there also a way to issue a click command on the currently disp

4条回答
  •  不知归路
    2021-01-17 13:03

    Try this:-

    AlertDialog.Builder alBuilder = new AlertDialog.Builder(this);
            alBuilder
                    .setMessage("Do you wamt to exit?")
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
    
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    dialog.cancel();
                                    // Write your code here for Yes
                                }
                            })
                    .setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
    
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    dialog.cancel();
                                    // Write your code here for No
                                }
                            });
            alBuilder.setCancelable(false);
            alBuilder.show();
    

提交回复
热议问题