0

我有以下代码。它显示了一个带有文本视图和一些按钮的对话框。按下按钮时,对话框应该关闭。但它没有。它有问题吗?我尝试了 dialog.dismiss 和 dialog.cancel 但它不起作用。

有什么解决办法?

AlertDialog.Builder alert = new AlertDialog.Builder(
                    Gestionarez.this);

            final TextView Dtv = new TextView(Gestionarez.this);
                 printeaza=new Button(Gestionarez.this);
                 stergere=new Button(Gestionarez.this);
                 trimitere=new Button(Gestionarez.this);
            final AlertDialog dialog = alert.create();

            trimitere.setText("Trimite");
            trimitere.setTextSize(10);
            trimitere.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                }
            });
            printeaza.setText("Printeaza");
            printeaza.setTextSize(10);
            printeaza.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                    dialog.cancel();
                }
            });
            stergere.setText("Sterge");
            stergere.setTextSize(10);
            stergere.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    boolean deleted = file.delete();

                    Toast.makeText(Gestionarez.this, str+ " a fost sters ",
                            Toast.LENGTH_SHORT).show();
                    lv2.invalidateViews();


                }
            });
            LinearLayout ldialog = new LinearLayout(Gestionarez.this);
            LinearLayout ldialogb = new LinearLayout(Gestionarez.this);
            ldialog.setOrientation(LinearLayout.VERTICAL);
            ldialogb.setOrientation(LinearLayout.HORIZONTAL);
            Dtv.setText(text);
            ldialog.addView(Dtv);
            ldialogb.addView(trimitere);
            ldialogb.addView(printeaza);
            ldialogb.addView(stergere);
            ldialog.addView(ldialogb);
            ldialogb.setGravity(Gravity.CENTER | Gravity.BOTTOM);
            Dtv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
            Dtv.setPadding(60, 60, 60, 60);
            alert.setView(ldialog);

            alert.show();

        }
    });

}
4

2 回答 2

2

好像您正在展示一个新创建的对话框,该对话框引用您不保留

alert.setView(ldialog);
alert.show();

您正试图关闭一个引用与该引用不同的对话框:

 dialog.dismiss();
 dialog.cancel();

从对话框中删除最后一行并将最后一行“ alert.show();”更改为

    dialog = alert.create();
    dialog.show();

现在您有了该对话框的引用,它将起作用....

于 2013-06-28T17:32:17.500 回答
0

dialog.finsh();那就试试dialog.dismiss();

或者这个 onClick :

if (dialog != null && dialog.isShowing()) {
        dialog.dismiss();
于 2013-06-28T17:46:41.360 回答