[英]How to remove view from AlertDialog Android
我正在制作一个带有自定义 EditText 字段的 ALert 对话框。
我创建了一个View
变量,然后将它与我的自定义EditText
字段相关联。
requestView = inflater.inflate(R.layout.send_request,null);
然后我将该视图添加到我的 AlertDialog
alert.setView(requestView);
之后,我将 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();
}
});
它工作正常。 但是当我再次按下按钮以执行警报对话框选项时,在警报对话框上按下取消选项后。
它因以下错误而崩溃。
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)
我虽然使视图为空并在单击按钮时再次添加视图将解决该问题。 但同样的问题一次又一次..
你的问题在这里:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(requestView);
在这种情况下, alert
不是对话框,而是构建器。 因此,每次当您尝试显示它时 - 它都会重建此对话框并尝试为其添加相同的视图 - requestView
。 因为它缓存在构建器中。 修复它 - 移动
requestView = inflater.inflate(R.layout.send_request,null);
alert.setView(requestView);
到您显示对话框的 OnClick 方法。 所以它应该是这样的:
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();
}
});
如果要使用现有视图,请使用它。
alert.setOnDismissListener(new OnDismissListener(){
((ViewGroup)requestView.getParent()).removeView(requestView);
});
上面的代码是我手工从 Kotlin 转换成 Java 的,请在使用前检查一下。
您想设置一个关闭侦听器。 我做了类似的事情:对话框应该设置一个关闭监听器
alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
((ViewGroup)requestView.getParent()).removeView(requestView);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.