简体   繁体   English

如何使AlertDialog消失?

[英]How to make an AlertDialog disappear?

I am trying to integrate Twitter with my application and I thought to add an AlertDialog which will appear after users press a button and this AlertDialog will ask the users if they want to post a tweet(The tweet will be the same for all users, like a promo tweet). 我正在尝试将Twitter与我的应用程序集成在一起,我想添加一个AlertDialog,它将在用户按下按钮后显示,并且该AlertDialog将询问用户是否要发布推文(所有用户的推文都相同,例如促销推文)。 My question is if there is any way to make the Alert Dialog disappear after a few moments, like 5 seconds. 我的问题是,是否有任何方法可以使警报对话框在5秒钟后消失。

I found this but I am not completely sure how it works. 我找到了这个,但是我不确定它是如何工作的。 I would appreciate any help. 我将不胜感激任何帮助。

you cannot access to UI from other threads so you must use handler to make modification on UI from the thread. 您无法从其他线程访问UI,因此必须使用处理程序从该线程对UI进行修改。

this is the code you use to make dialog disappear: 这是使对话框消失的代码:

 d.dismiss(); 

and this is the handler code 这是处理程序代码

Handler handler= new Handler()

use the method from link and send to it time and dialog object 使用链接中的方法并向其发送时间和对话框对象

Declare your Handler that will deal to dismiss the dialog after some time, and declare thread that will pause the current thread and keep dialog for a some amount of time. 声明你的Handler ,将处理解雇一段时间后的对话框,并宣布线程将暂停当前线程,并保持对话的时间一定量。 In the Runnable of the thread make a statement that will sleep the thread for a some time, eg Thread.sleep(5000); 在线程的Runnable中,执行一个使该线程休眠一段时间的语句,例如Thread.sleep(5000); then use the Handler object to sendMessage() ;, in the Handler onHandleMessage() method just do dialog.dissmiss(); 然后使用Handler对象来sendMessage() ;在Handler的onHandleMessage()方法中,只需执行dialog.dissmiss(); After some operation, at point where you want keep the dialog, and start the thread using myThread.start(); 完成一些操作后,在要保留对话框的位置,然后使用myThread.start();启动线程myThread.start(); . Mind the instantiation of the thread each time when/where you need. 每次需要/在任何时候注意线程的实例化。

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

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