简体   繁体   English

如何从 AlertDialog Android 中删除视图

[英]How to remove view from AlertDialog Android

I am making an ALert Dialog with custom EditText Field.我正在制作一个带有自定义 EditText 字段的 ALert 对话框。

I made a View variable and then associated it with my custom EditText field.我创建了一个View变量,然后将它与我的自定义EditText字段相关联。

requestView = inflater.inflate(R.layout.send_request,null);

Then I added that view to my AlertDialog然后我将该视图添加到我的 AlertDialog

alert.setView(requestView);

And after that I added the onClick Method To My Button to perform the alert Dialog action..之后,我将 onClick 方法添加到我的按钮以执行警报对话框操作..

chatRequestbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        request = requestMsg.getText().toString();

                        send();

                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }
                });

                alert.show();

            }
        });

It worked correctly.它工作正常。 But after pressing cancel option on alert dialog when I press the button again to perform the alert dialog option.但是当我再次按下按钮以执行警报对话框选项时,在警报对话框上按下取消选项后。

It crashes with the following error.它因以下错误而崩溃。

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                           at android.view.ViewGroup.addViewInner(ViewGroup.java:4417)
                                                                           at android.view.ViewGroup.addView(ViewGroup.java:4258)
                                                                           at android.view.ViewGroup.addView(ViewGroup.java:4230)
                                                                           at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:647)
                                                                           at android.support.v7.app.AlertController.setupView(AlertController.java:463)
                                                                           at android.support.v7.app.AlertController.installContent(AlertController.java:226)
                                                                           at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
                                                                           at android.app.Dialog.dispatchOnCreate(Dialog.java:395)
                                                                           at android.app.Dialog.show(Dialog.java:294)
                                                                           at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
                                                                           at com.buckydroid.anonchat.User$3.onClick(User.java:86)
                                                                           at android.view.View.performClick(View.java:5637)
                                                                           at android.view.View$PerformClick.run(View.java:22433)
                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6126)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

I though making the view null and adding the view again while clicking on the button will fix the issue.我虽然使视图为空并在单击按钮时再次添加视图将解决该问题。 But same problem again and again..但同样的问题一次又一次..

Your problem is here:你的问题在这里:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(requestView);

In this case alert is not the dialog, but a builder.在这种情况下, alert不是对话框,而是构建器。 So every time when you're trying to show it - it rebuilds this dialog and trying to add same view for it - requestView .因此,每次当您尝试显示它时 - 它都会重建此对话框并尝试为其添加相同的视图 - requestView Because it is cached in the builder.因为它缓存在构建器中。 To fix it - move修复它 - 移动

requestView = inflater.inflate(R.layout.send_request,null);
alert.setView(requestView);

to your OnClick method where you're showing the dialog.到您显示对话框的 OnClick 方法。 So it should look like this:所以它应该是这样的:

chatRequestbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              requestView = inflater.inflate(R.layout.send_request,null);
              alert.setView(requestView);

                alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        request = requestMsg.getText().toString();

                        send();

                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }
                });

                alert.show();

            }
        });

If you want to use an existing View, use this.如果要使用现有视图,请使用它。

alert.setOnDismissListener(new OnDismissListener(){
    ((ViewGroup)requestView.getParent()).removeView(requestView);
});

I converted the above code from Kotlin to Java by hand, plz check before use.上面的代码是我手工从 Kotlin 转换成 Java 的,请在使用前检查一下。

You want to set an on dismiss listener.您想设置一个关闭侦听器。 I did something like: The dialog should set an on Dismiss listener我做了类似的事情:对话框应该设置一个关闭监听器

alert.setOnDismissListener(new DialogInterface.OnDismissListener()  {
       @Override
       public void onDismiss(DialogInterface dialog) {
              ((ViewGroup)requestView.getParent()).removeView(requestView);
       }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM