我怎样才能杀死一个自定义对话框

系统教程 行业动态 更新时间:2024-06-14 16:57:40
我怎样才能杀死一个自定义对话框 - 在它被创建之前?(How can I kill a custom dialog - before it's been created?)

显然,我有一个我喜欢称之为“鸡还是蛋”综合征的例子。 我试图找到一种方法来杀死一个警告对话框,该对话框在该过程结束前不会创建。

我想要做的是:当被问及“你想重新下载这个图库吗?”时,我想在我得到“是”答案时杀死潜在的弹出窗口(警报/构建器)。 但是,当我得到“否”的答案时不行。 换句话说,我想在那里放一个alert.dismiss()...但我不能,因为它不会在流程结束之前创建。

那有意义吗? 我试图找到另一种方式来向用户展示这一点,我正在绞尽脑汁。

这是我的代码:

public void showGalleriesDialogue(String galleriesArray) { Log.v("Status","Ran alert dialogue routine."); final CharSequence[] items = TextUtils.split(galleriesArray, "\\^"); final AlertDialog.Builder builder = new AlertDialog.Builder(MainMenu.this); builder.setTitle("Choose a Gallery to download:"); builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { final String galleryToDownload = (String) items[item].toString(); // Check to ensure that this doesn't all ready exist File checkExisists = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/pm-pvault/" + galleryToDownload); Log.v("Status","Checking to ensure that '"+checkExisists+"' does not exist."); if(checkExisists.exists()) { AlertDialog.Builder existsBuilder = new AlertDialog.Builder(MainMenu.this); existsBuilder.setTitle("But wait!"); existsBuilder.setMessage("This gallery already exists. Do you want to RE-INSTALL it?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Preview stuff, then go get it. GlobalDataStore.retrycount = 0; preview(galleryToDownload); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Never mind. dialog.cancel(); } }); AlertDialog existsAlert = existsBuilder.create(); existsAlert.show(); } //OR ELSE else if(galleryToDownload.contains("@")==true) { AlertDialog.Builder doItNasty = new AlertDialog.Builder(MainMenu.this); doItNasty.setTitle("DISCLAIMER"); doItNasty.setMessage(R.string.nastyDisclaimer) .setCancelable(false) .setPositiveButton("I AGREE", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Download stuff, go get it. GlobalDataStore.retrycount = 0; preview(galleryToDownload); } }) .setNegativeButton("DISAGREE", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Never mind. dialog.cancel(); } }); AlertDialog nastyAlert = doItNasty.create(); nastyAlert.show(); } else { // Out of IF conditions to meet. Go download it now. GlobalDataStore.retrycount = 0; preview(galleryToDownload); } } // END OR ELSE } }); final AlertDialog alert = builder.create(); alert.show(); }

Clearly I have a case of what I like to call "chicken or the egg" syndrome. I am trying to find a way to kill an alert dialog which isn't created until the end of the process.

What I am trying to do: When asked "Do you want to RE-DOWNLOAD this gallery?", I want to KILL the underlying pop-up (alert/builder) when I get a "Yes" answer. But not when I get a "No" answer. In other words, I would like to put an alert.dismiss() in there ... but I can't, because it's not created until the end of the process.

Does that make sense? I'm racking my brain on this trying to find another way to present this to a user.

Here is my code:

public void showGalleriesDialogue(String galleriesArray) { Log.v("Status","Ran alert dialogue routine."); final CharSequence[] items = TextUtils.split(galleriesArray, "\\^"); final AlertDialog.Builder builder = new AlertDialog.Builder(MainMenu.this); builder.setTitle("Choose a Gallery to download:"); builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { final String galleryToDownload = (String) items[item].toString(); // Check to ensure that this doesn't all ready exist File checkExisists = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/pm-pvault/" + galleryToDownload); Log.v("Status","Checking to ensure that '"+checkExisists+"' does not exist."); if(checkExisists.exists()) { AlertDialog.Builder existsBuilder = new AlertDialog.Builder(MainMenu.this); existsBuilder.setTitle("But wait!"); existsBuilder.setMessage("This gallery already exists. Do you want to RE-INSTALL it?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Preview stuff, then go get it. GlobalDataStore.retrycount = 0; preview(galleryToDownload); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Never mind. dialog.cancel(); } }); AlertDialog existsAlert = existsBuilder.create(); existsAlert.show(); } //OR ELSE else if(galleryToDownload.contains("@")==true) { AlertDialog.Builder doItNasty = new AlertDialog.Builder(MainMenu.this); doItNasty.setTitle("DISCLAIMER"); doItNasty.setMessage(R.string.nastyDisclaimer) .setCancelable(false) .setPositiveButton("I AGREE", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Download stuff, go get it. GlobalDataStore.retrycount = 0; preview(galleryToDownload); } }) .setNegativeButton("DISAGREE", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Never mind. dialog.cancel(); } }); AlertDialog nastyAlert = doItNasty.create(); nastyAlert.show(); } else { // Out of IF conditions to meet. Go download it now. GlobalDataStore.retrycount = 0; preview(galleryToDownload); } } // END OR ELSE } }); final AlertDialog alert = builder.create(); alert.show(); }

最满意答案

onClick事件是一个回调,这意味着该对话框在被调用时已经存在,所以取消它应该没有问题。

问题是你正在重新定义变量'dialog',所以你无法访问'outer'对话框。

尝试这样做:

.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface redownloadDialog, int id) { // Never mind. redownloadDialog.cancel(); dialog.cancel(); } });

The onClick event is a callback, which means the dialog already exists when it's called, so there should be no problem cancelling it.

The problem is you're redefining the variable 'dialog', so you have no way to access the 'outer' dialog.

Try doing it something like:

.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface redownloadDialog, int id) { // Never mind. redownloadDialog.cancel(); dialog.cancel(); } });

更多推荐

本文发布于:2023-04-13 12:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/f60229b9a7ab806119aa580b40488bef.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   对话框

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!