簡體   English   中英

AlertDialog沒有在OnClickListener中顯示

[英]AlertDialog not showing inside OnClickListener

我想在alertDialog顯示OnClickListener 但是當我在onclickListener使用以下代碼時, alertDialog沒有顯示出來。 任何幫助都會很棒。

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();
                                    }});

使用它會起作用

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();

在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