简体   繁体   English

AlertDialog没有在OnClickListener中显示

[英]AlertDialog not showing inside OnClickListener

I want to display an alertDialog inside an OnClickListener . 我想在alertDialog显示OnClickListener But the alertDialog is not showing up when I use the following code inside the onclickListener . 但是当我在onclickListener使用以下代码时, alertDialog没有显示出来。 Any help would be great. 任何帮助都会很棒。

final AlertDialog alertDialog = new AlertDialog.Builder(MyClass.this).create();
alertDialog.setTitle("Info:");
String alert1 = "First Name: " + Fname;
String alert2 = "Surname: " + Sname;
String alert3 = "Id: " + tId;
String alert4 = "Password: " + tPassword;
alertDialog.setMessage(alert1 +"\n"+ alert2 +"\n"+ alert3+"\n" + alert4);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                                                startActivity(intent);
                                            }
                                        });

                                        alertDialog.show();
                                    }});

Use this it will work 使用它会起作用

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("ALERTTILESTRING")
                .setMessage("alertNameString")
                .setCancelable(false)
                .setPositiveButton("OK",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();

add the below code inside the onclicklistner : 在onclicklistner中添加以下代码:

 AlertDialog.Builder dialog1 = new AlertDialog.Builder(this);
            dialog1.setTitle("Info:");
            String alert1 = "First Name: " + Fname;
            String alert2 = "Surname: " + Sname;
            String alert3 = "Id: " + tId;
            String alert4 = "Password: " + tPassword;
            dialog1.setMessage(alert1 + "\n" + alert2 + "\n" + alert3 + "\n" + alert4);
            dialog1.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    startActivity(intent);
                }
            });
            dialog1.show();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle);
            builder.setTitle("");
            builder.setMessage("");
            builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //do stuff

                }
            });
            builder.setNegativeButton("CLOSE", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int id){
                      //do stuff
                }
            });
            builder.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM