如何解除此警报对话框?

时间:2017-07-22 07:27:49

标签: android firebase-realtime-database alertdialog dismiss

我已经在这个工作了一个小时,我无法弄清楚为什么即使点击其中一个按钮后我也无法解除对话。如果我把b.dismiss()放在按钮之后,对话框甚至不会显示。

   private AlertDialog b;

   // custom dialog
    AlertDialog.Builder dialogBuilder = new 
    AlertDialog.Builder(MapsActivity.this);
    LayoutInflater inflater = MapsActivity.this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
    dialogBuilder.setView(dialogView);

  Button reject = (Button) dialogView.findViewById(R.id.reject_btn);
  Button accept = (Button) dialogView.findViewById(R.id.accept_btn);

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


    accept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            b.dismiss();



        }
    });

    reject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            b.dismiss();


        }
    });

我试过在

中解雇它
   dialogBuilder.setPositiveButton("Done", new 
     DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //do something with edt.getText().toString();
     }
});
dialogBuilder.setNegativeButton("Cancel", new 
  DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //pass
    }
});

那不起作用

如果你能找到我错过的东西,你会非常感谢你的帮助!

我注意到,如果我解雇它大约60次就会解散,这意味着,我的代码创建了60次对话框,这可能是什么原因?

修改

4 个答案:

答案 0 :(得分:1)

试试这个

     dialogBuilder.setPositiveButton("Done", new 
     DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //do something with edt.getText().toString();
        dialogBuilder.dismiss(); 
        //  or
         dialogBuilder.cancel();
     }
});
dialogBuilder.setNegativeButton("Cancel", new 
  DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    dialogBuilder.dismiss(); 
      // or
     dialogBuilder.cancel();
    }
});

答案 1 :(得分:1)

尝试此操作以关闭对话框

dialogBuilder.setPositiveButton("Done", new
     DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //do something with edt.getText().toString();
            dialog.dismiss();
        }
});

dialogBuilder.setNegativeButton("Cancel", new
     DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss();
       }
});

答案 2 :(得分:0)

试试这个

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("message");
        builder.setPositiveButton("Yes", new
 DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
        dialog.dismiss();

        });

        builder.setNegativeButton("NO",new
 DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
        dialog.dismiss();
        });
        builder.show();

答案 3 :(得分:0)

我不知道您编写代码的位置以启动对话框。我复制了你的代码。这可以按预期工作。

public class MainActivity extends AppCompatActivity {

    private AlertDialog b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // custom dialog
        AlertDialog.Builder dialogBuilder = new
                AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
        dialogBuilder.setView(dialogView);

        Button reject = (Button) dialogView.findViewById(R.id.reject_btn);
        Button accept = (Button) dialogView.findViewById(R.id.accept_btn);

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


        accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                b.dismiss();
            }
        });

        reject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                b.dismiss();
            }
        });
    }
}

以下是XML。同样是一个非常基本的匹配代码

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:id="@+id/reject_btn"
        android:text="Reject"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="wrap_content"
        android:id="@+id/accept_btn"
        android:text="Accept"
        android:layout_height="wrap_content" />
</LinearLayout>