按下按钮关闭自定义警告对话框

32

我无法关闭我的警报对话框。 我使用布局填充器来创建对话框,因此我不确定在完成后如何关闭它。 以下是代码:

AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
DialogInterface dia = new DialogInterface();

//Create a custom layout for the dialog box
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);

TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
Button add_item = (Button)layout.findViewById(R.id.add_button);

add_item.setOnClickListener(new OnClickListener()
{
        @Override
        public void onClick(View v)
        {
        //Need this to close the alert dialog box
        }
});

title.setText(workout_items[position]);
dialog.setView(layout);
dialog.show();

我无法调用finish方法,因为这将关闭启动对话框的ListView且我不能使用dialog.dismiss方法。

你认为我该怎么做来解决这个问题呢?

11个回答

48
AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
DialogInterface dia = new DialogInterface();

//Create a custom layout for the dialog box
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);

TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
Button add_item = (Button)layout.findViewById(R.id.add_button);

title.setText(workout_items[position]);
dialog.setView(layout);

AlertDialog alertDialog = dialog.create();

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


alertDialog.show();

2
变量alertDialog在内部类中被访问。需要声明为final。 - Anton Chikin
1
DialogInterface dia 是用来做什么的? - ghchoi

18

试试这个:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.brush_opts_dialog,null);

    builder.setView(dialogView);


    closeBtn = (Button)dialogView.findViewById(R.id.close_btn);


   final AlertDialog dialog = builder.create();

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

    dialog.show();

您应该使用AlertDialog.Builder来“构建”警告对话框本身。然后,调用AlertDialog.Builder对象上的create()方法。此方法返回一个AlertDialog对象,允许您调用dismiss()。

您不应该多次创建它,也不需要使用任何DialogInterface对象。这对我有效。请告诉我您的情况如何。


你对于调用create()的解释正是我所需要的。 - zeeshan

16

像这样做:

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

3

我已经修改了你的代码,请检查一下。

   AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
    DialogInterface dia = new DialogInterface();

    //Create a custom layout for the dialog box
    LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);

    TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
    Button add_item = (Button)layout.findViewById(R.id.add_button);



    final AlertDialog Dial = alertDialog.create();
    title.setText(workout_items[position]);
    Dial.setView(layout);

    AlertDialog alertDialog = dialog.create();

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


    Dial.show();

刚刚添加

最后创建AlertDialog的代码为:final AlertDialog Dial = alertDialog.create(); 并将 dialog.setView(layout); 改为 Dial.setView(layout);

现在只需在onclick监听器中调用Dial.dismiss();即可... 对我来说运行良好。


1
@Abdus.. 你为什么调用了 create() 两次? - DJhon

2

在你的类中使用这段代码:

Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
((Button) dialog.findViewById(R.id.button))
   .setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog.dismiss();
    }
});
dialog.show();

创建一个 custom.xml 文件,然后将以下代码粘贴到其中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Ok "/>
</RelativeLayout>

源代码:https://www.mkyong.com/android/android-custom-dialog-example/

如果您不喜欢上面的代码,请参考下面的链接: http://thedeveloperworldisyours.com/android/prevent-alertdialog-closing-button-clicked/


0
你需要实现DialogInterface.OnClickListener而不是View.OnClickListener。这样dialog.dismiss()就可以使用了。
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
       dialog.dismiss();
}
}

0

你收到错误,尝试这段代码

   dialog.setView(layout);

   final AlertDialog alertDialog = dialog.create();

   add_item.setOnClickListener(new OnClickListener(){
    @Override
     public void onClick(View v)
        {
          if (alertDialog != null && alertDialog.isShowing()) {
              alertDialog.dismiss();
        }
      }
    });
   alertDialog.show();

0
     final viewProgressDialogue viewProgressDialogue = new viewProgressDialogue(DebugActivity.this);
        viewProgressDialogue.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.d("response","im calling");
                viewProgressDialogue.dismiss();
            }
        }, 5000);
> Make it one Instance so that it will work
It Will in the Fragment too.

for Fragment Use
    final viewProgressDialogue viewProgressDialogue = new viewProgressDialogue(getActivity());
        viewProgressDialogue.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.d("response", "im calling");
                viewProgressDialogue.dismiss();
            }
        }, 5000);
> viewProgressDialogue Class
public class viewProgressDialogue extends Dialog {
    public viewProgressDialogue(@NonNull Context context) {
        super(context);

        WindowManager.LayoutParams wlmp = getWindow().getAttributes();
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        wlmp.gravity = Gravity.CENTER_HORIZONTAL;
        getWindow().setAttributes(wlmp);
        setTitle(null);
        setCancelable(false);
        setOnCancelListener(null);

        View view = LayoutInflater.from(context).inflate(
                R.layout.custom_progress, null);
        setContentView(view);


    }
}

0
在您的onclick监听器中尝试使用dialog.cancel();,它应该可以工作。

0

以上答案有一点小变化,就是检查一下是否在builder.setView(...);方法之前写了AlertDialog alertDialog = builder.create();,如果是的话,那么就把它写在builder.setView(...);之后就可以了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接