如何以编程方式删除AlertDialog [英] How to remove AlertDialog programmatically
本文介绍了如何以编程方式删除AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在android应用程序中,我向用户显示一个AlertDialog,没有按钮,只有一条消息.如何以编程方式销毁AlertDialog,以使其不再可见?我尝试使用cancel()和dismiss(),但是它们不起作用,视图仍然存在.
In an android application, I'm showing to the user an AlertDialog with no buttons, just a message. How can I destroy the AlertDialog programmatically so that it's not visible anymore? I tried with cancel() and dismiss() but they are not working, the view remains there.
AlertDialog.Builder test = new AlertDialog.Builder(context);
test.setTitle("title");
test.setCancelable(true);
test.setMessage("message...");
test.create().show();
然后我尝试了
test.show().cancel()
和
test.show().dismiss()
但不起作用.
推荐答案
您应该引用AlertDialog本身,而不是生成器.
You should refer to the AlertDialog itself, not the builder.
AlertDialog.Builder test = new AlertDialog.Builder(context);
test.setTitle("title");
test.setCancelable(true);
test.setMessage("message...");
ALertDialog testDialog = test.create();
testDialog.show(); // to show
testDialog.dismiss(); // to dismiss
这篇关于如何以编程方式删除AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文