AlertDialog解雇不起作用

时间:2016-02-22 12:54:17

标签: android android-alertdialog

我有以下AlertDialog:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
dialogBuilder.setTitle(R.string.title);
dialogBuilder.setMessage(mContext.getString(R.string.message));
dialogBuilder.setPositiveButton(R.string.positive, new MyOnClickListener());
dialogBuilder.setNegativeButton(R.string.negative, new MyOnClickListener());
dialogBuilder.show();

使用此ClickListener

public static class MyOnClickListener implements DialogInterface.OnClickListener{
    @Override
    public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss();
    }
  }

我希望在单击任一按钮时关闭对话框,但对话框保持打开状态。

我调试了onClick方法和行

dialog.dismiss() 

正在执行,但没有任何反应。

我哪里出错或如何解决这个问题?

6 个答案:

答案 0 :(得分:5)

在顶部声明您的AlertDialog,如:

private AlertDialog myAlertdialog;

而不是将您的dialogBuilder.show();替换为

myAlertDialog = dialogBuilder.create();
myAlertDialog.show();

比你可以致电myAlertDialog.dismiss();

答案 1 :(得分:1)

试试这个

AlertDialog alertDialog = new AlertDialog.Builder(
                    AlertDialogActivity.this).create();

    // Setting Dialog Title
    alertDialog.setTitle("Alert Dialog");

    // Setting Dialog Message
    alertDialog.setMessage("Welcome to AndroidHive.info");

    // Setting Icon to Dialog
    alertDialog.setIcon(R.drawable.tick);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // Write your code here to execute after dialog closed
            Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
            }
    });

    // Showing Alert Message
    alertDialog.show();

答案 2 :(得分:1)

使用interface创建自己的对话框按钮时,尝试使用以下代码点击监听器。

<强> MyOnClickListener.java

import android.content.DialogInterface;

public interface MyOnClickListener extends DialogInterface.OnClickListener {
    @Override
    public void onClick(DialogInterface dialog, int which);
}

您的对话框代码:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
    dialogBuilder.setTitle("You Title");
    dialogBuilder.setMessage("Here Is your message");
    dialogBuilder.setPositiveButton("YES", new MyOnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "YES Clicked", Toast.LENGTH_LONG).show();
dialog.dismiss();
        }
    });
    dialogBuilder.setNegativeButton("NO", new MyOnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "NO Clicked", Toast.LENGTH_LONG).show();
dialog.dismiss();
        }
    });
    dialogBuilder.show();

<强>输出:

打开对话框:

Your dialog

执行OnClick对话框:

OnClick of dialog

我希望它会帮助你。

答案 3 :(得分:0)

而不是dialog.dismiss(),请使用dialoginterface.dismiss()

我已经在下面展示了一个例子

public void onClick(DialogInterface dialogInterface, int i) 
{
    dialogInterface.dismiss();
} 

答案 4 :(得分:0)

试试这个......它对我有用......你可以在你的对话中使用这个方法 或者您可以简单地使用 alert.dismiss(); 来关闭对话框....或者只是将对话框方法放在代码中并调用方法,即 MyAlertDialogBox(); 无论你想要一个对话框。

AlertDialog alert;
// Dialog method .
private void MyAlertDialogBox(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage("Welcome ! This is a Alert dialog")
            .setCancelable(false)
            //You can name you strings according to your preference here 
            //or the String.xml file
            .setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener()
            {
                public void onClick(final DialogInterface dialog, int id) {
                    Toast.makeText(MyActivityName.this, "Toast message 
 when you click Yes ", Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton( getResources().getString(R.string.no), 
 new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Toast.makeText(MyActivityName.this, "Toast message 
 when you click No ", Toast.LENGTH_SHORT).show();

   //                        To dismiss your alert dialog box on click of 
     No button (or you can say the negative button)
                    alert.dismiss();
                }
            });

    //Creating dialog box
    alert = builder.create();
    /*   To give dialog box title   
        alert.setTitle(getResources().getString(R.string.folder));*/

     /*    to set icon in alert dialog box
    alert.setIcon(R.drawable.ic_create_new_folder_white_24dp);*/
    alert.show();
}

答案 5 :(得分:-1)

当您将行 <table style="width:100%"> <tr> <td>Data 1_1</td> <td>Data 1_2</td> <td>Data 1_3</td> </tr> <tr> <td>Data 2_1</td> <td>Data 2_2</td> <td>Data 2_3</td> </tr> </table> <button type="button">Download CSV!</button> 更改为dialogBuilder.show()时,它应该正常工作。